gitignore - Specifies intentionally untracked files to ignore (指定要忽略的故意未跟踪的文件)

mac2024-07-24  56

gitignore - Specifies intentionally untracked files to ignore (指定要忽略的故意未跟踪的文件)

https://www.php.cn/manual/view/34942.html https://git-scm.com/docs

1. SYNOPSIS

$XDG_CONFIG_HOME/git/ignore, $GIT_DIR/info/exclude, .gitignore

2. DESCRIPTION

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details. gitignore 文件指定 Git 应该忽略的故意未记录的文件。Git 已经跟踪的文件不受影响。有关详细信息,请参阅下面的注释。

Each line in a gitignore file specifies a pattern. When deciding whether to ignore a path, Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome): gitignore 文件中的每一行都指定一个模式。在决定是否忽略路径时,Git 通常会检查 gitignore 来自多个来源的模式,并按以下优先级顺序从高到低 (在一个优先级内,最后一个匹配模式决定结果):

intentionally [ɪn'tɛnʃənəli]:adv. 故意地,有意地 Patterns read from the command line for those commands that support them. (从支持它们的命令行读取 - 模式。)Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file. These patterns match relative to the location of the .gitignore file. A project normally includes such .gitignore files in its repository, containing patterns for files generated as part of the project build. (从与路径相同的目录中或任何父目录中的 .gitignore 文件读取,其中较高级别文件 (直到工作树的最高级别) 中的模式被较低级别文件中的模式覆盖,直至包含文件的目录。这些模式相对于 .gitignore 文件的位置匹配。项目通常在其存储库中包含此类 .gitignore 文件,其中包含作为项目构建的一部分而生成的文件的模式。)Patterns read from $GIT_DIR/info/exclude. (从 $GIT_DIR/info/exclude 读取 - 模式。)Patterns read from the file specified by the configuration variable core.excludesFile. (从配置变量 core.excludesFile 指定的文件中读取 - 模式。)

Which file to place a pattern in depends on how the pattern is meant to be used. 放置模式的文件取决于模式的使用方式。

Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that all developers will want to ignore) should go into a .gitignore file. (应该由版本控制并通过克隆 (即所有开发人员都想忽略的文件) 分发到其他存储库的模式应放入 .gitignore 文件中。)Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow) should go into the $GIT_DIR/info/exclude file. (特定于特定存储库但不需要与其他相关存储库共享的模式 (例如,位于存储库内但特定于一个用户工作流的辅助文件) 应放入 $GIT_DIR/info/exclude file 文件中。)Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead. (用户在所有情况下都希望 Git 忽略的模式 (例如,由用户选择的编辑器生成的备份或临时文件) 通常会进入用户 ~/.gitconfig 中的 core.excludesFile 指定的文件中。其默认值为 $XDG_CONFIG_HOME/git/ignore。如果未设置 $XDG_CONFIG_HOME 或为空,则使用 $HOME/.config/git/ignore。)

The underlying Git plumbing tools, such as git ls-files and git read-tree, read gitignore patterns specified by command-line options, or from files specified by command-line options. Higher-level Git tools, such as git status and git add, use patterns from the sources specified above. 底层 Git 管道工具 (such as git ls-files and git read-tree) 读取 gitignore 由命令行选项指定的模式或从命令行指定的文件选项。更高级的 Git 工具,such as git status and git add,使用来自上面指定源的模式。

plumb [plʌm]:vt. 使垂直,探测,探索 n. 垂直,铅锤 adv. 恰恰,正,垂直地 vi. 当管子工 adj. 垂直的

3. PATTERN FORMAT

A blank line matches no files, so it can serve as a separator for readability. (空白行不匹配任何文件,因此它可以作为可读性的分隔符。) separator [ˈsepəreɪtə(r)]:n. 分离器,分隔符 readability [ˌriːdəˈbɪləti]:n. 可读性,易读 backslash [ˈbækslæʃ]:n. 反斜杠,反斜线符号 quote [kwəʊt]:vt. 引述,引用,举例说明,报价,以...为证,为 (企业的股份) 上市 vi. 引述,引用 n. 引文,语录,开价,(股票) 牌价,引号,(建筑或维修的) 报价单 trail [treɪl]:v. 跟踪,追踪,(被) 拖,磨蹭 n. 小径,路径,踪迹,足迹

以斜杠 / 开头表示目录。

Trailing spaces are ignored unless they are quoted with backslash (\). (尾部空格被忽略,除非用反斜杠 (\) 引用。)

A line starting with # serves as a comment. Put a backslash (\) in front of the first hash for patterns that begin with a hash. (以 # 开头的行用作注释。)

An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash (\) in front of the first ! for patterns that begin with a literal !, for example, \!important!.txt. (可选的前缀 !,它会否定模式。先前模式排除的所有匹配文件将再次包含在内。如果该文件的父目录被排除,则无法重新包含该文件。由于性能原因,Git 不会列出被排除的目录,因此包含在文件上的任何模式 (无论它们在何处定义) 均无效。对于第一个 ! 开头的模式,请在第一个 ! 前面加上反斜杠 (\),for example, \!important!.txt。)

如果排除了该文件的父级目录,则使用 ! 也不会再次被包含。

The slash / is used as the directory separator. Separators may occur at the beginning, middle or end of the .gitignore search pattern. (斜杠 / 用作目录分隔符。分隔符可以出现在 .gitignore 搜索模式的开始、中间或结尾。)

If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level. (如果在模式的开头或中间 (或两者) 都存在分隔符,则该模式是相对于特定 .gitignore 文件本身的目录级别的。否则,模式也可能在 .gitignore 级别以下的任何级别匹配。)

particular [pəˈtɪkjʊlə(r)]:adj. 特别的,详细的,独有的,挑剔的 n. 详细说明,个别项目

If there is a separator at the end of the pattern then the pattern will only match directories, otherwise the pattern can match both files and directories. (如果模式末尾有分隔符,则该模式将仅匹配目录,否则该模式可以同时匹配文件和目录。)

The pattern foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git). 模式 foo/ 会匹配目录 foo 及其下的路径,但不会匹配 a regular file or a symbolic link foo (这与 pathspec 在 Git 中的一般工作方式一致)。

For example, a pattern doc/frotz/ matches doc/frotz directory, but not a/doc/frotz directory; however frotz/ matches frotz and a/frotz that is a directory (all paths are relative from the .gitignore file). (例如模式 doc/frotz/ 与目录 doc/frotz 匹配,而不与目录 a/doc/frotz 匹配。但是 frotz/ 匹配作为目录的 frotz and a/frotz (所有路径都是相对于 .gitignore 文件的相对路径)。)

The pattern doc/frotz and /doc/frotz have the same effect in any .gitignore file. In other words, a leading slash is not relevant if there is already a middle slash in the pattern. (模式 doc/frotz and /doc/frotz 在任何 .gitignore 文件中具有相同的效果。换句话说,如果模式中已经存在中间斜线,则前导斜线将不相关。)

The pattern foo/*, matches foo/test.json (a regular file), foo/bar (a directory), but it does not match foo/bar/hello.c (a regular file), as the asterisk in the pattern does not match bar/hello.c which has a slash in it. (模式 foo/* 匹配 foo/test.json (a regular file), foo/bar (a directory),但不匹配 foo/bar/hello.c (a regular file),因为该模式中的星号与其中带有斜线的 bar/hello.c 不匹配。)

The pattern hello.* matches any file or folder whose name begins with hello. If one wants to restrict this only to the directory and not in its subdirectories, one can prepend the pattern with a slash, i.e. /hello.*; the pattern now matches hello.txt, hello.c but not a/hello.java. 模式 hello.* 匹配名称以 hello. 开头的任何文件或文件夹。如果只想将此限制于目录而不是其子目录,则可以在模式前面加上斜杠,即 /hello.*,模式现在匹配 hello.txt,hello.c,但不匹配 a/hello.java。

An asterisk * matches anything except a slash. The character ? matches any one character except /. The range notation, e.g. [a-zA-Z], can be used to match one of the characters in a range. See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description. (星号 * 匹配除斜杠之外的所有内容。字符 ? 匹配除 / 之外的任何一个字符。范围符号,例如 [a-zA-Z] 可用于匹配范围中的一个字符。See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description.)

Two consecutive asterisks (**) in patterns matched against full pathname may have special meaning: (匹配完整路径名的模式中的两个连续星号 (**) 可能有特殊含义:)

A leading ** followed by a slash means match in all directories. For example, **/foo matches file or directory foo anywhere, the same as pattern foo. **/foo/bar matches file or directory bar anywhere that is directly under directory foo. (前导 ** 后跟斜杠表示在所有目录中均匹配。例如 **/foo 与任何位置文件或目录 foo 匹配,与模式 foo 相同。**/foo/bar 与直接位于目录 foo 下任何位置文件或目录 bar 匹配。)

A trailing /** matches everything inside. For example, abc/** matches all files inside directory abc, relative to the location of the .gitignore file, with infinite depth. (尾部 /** 匹配内部的所有内容。例如 abc/** 匹配目录 abc 内的所有文件,相对于 .gitignore 文件的位置,具有无限深度。)

A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, a/**/b matches a/b, a/x/b, a/x/y/b and so on. (斜杠后跟两个连续的星号,则斜线匹配零个或多个目录。例如 a/**/b 匹配 a/b, a/x/b, a/x/y/b 等。)

Other consecutive asterisks are considered regular asterisks and will match according to the previous rules. (其他连续的星号被视为常规星号,并将根据之前的规则进行匹配。)

consecutive [kənˈsekjətɪv]:adj. 连贯的,连续不断的 asterisk [ˈæstərɪsk]:n. 星号,星号键 vt. 注上星号,用星号标出 regular [ˈreɡjələr]:adj. 定期的,有规律的,合格的,整齐的,普通的 n. 常客,正式队员,中坚分子 adv. 定期地,经常地 slash [slæʃ]:vt. 猛砍,鞭打,严厉批评,大幅度裁减或削减 vi. 猛砍,严厉批评 n. 削减,斜线,猛砍,砍痕,沼泽低地

4. CONFIGURATION

The optional configuration variable core.excludesFile indicates a path to a file containing patterns of file names to exclude, similar to $GIT_DIR/info/exclude. Patterns in the exclude file are used in addition to those in $GIT_DIR/info/exclude. 可选的配置变量 core.excludesFile 指示文件的路径,该路径包含要排除的文件名模式,类似于 $GIT_DIR/info/exclude。除了 $GIT_DIR/info/exclude 中的模式外,还使用了排除文件中的模式。

5. NOTES

The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked. (gitignore 文件的目的是确保未被 Git 跟踪的某些文件保持未被跟踪。) To stop tracking a file that is currently tracked, use git rm --cached. (要停止跟踪当前跟踪的文件,请使用 git rm --cached。)

.gitignore 只能忽略那些原来没有被跟踪的文件,如果某些文件已经被纳入了版本管理中,则修改 .gitignore 是无效的。可以先把本地缓存删除 (改变成 untracked 状态),然后再提交。

git rm -r --cached . 方法调用要慎重,因为该命令会删除暂存区或分支上的文件,但本地又需要使用,只是不希望这个文件被版本控制。

git rm -r --cached . git add . git commit -m 'update .gitignore'
最新回复(0)