.gitignore

Git 将工作副本中的每个文件视为以下三种情况之一:

  1. tracked - 先前已暂存或提交的文件;
  2. untraced - 尚未_暂存或提交的文件;
  3. ignored - Git 被明确告知要忽略的文件。

忽略的文件通常是构建工件和机器生成的文件,它们可以从您的存储库源中派生,或者不应以其他方式提交。一些常见的例子是:

  • 依赖缓存,例如/node_modulesor的内容/packages
  • 编译后的代码,例如.o.pyc.class文件
  • 构建输出目录,例如/bin, /out, 或/target
  • 运行时生成的文件,例如、或.log .lock .tmp
  • 隐藏的系统文件,例如.DS_StoreThumbs.db
  • 个人IDE配置文件,例如.idea/workspace.xml

忽略的文件在一个名为的特殊文件中进行跟踪,.gitignore该文件已签入存储库的根目录。没有明确的 git ignore 命令:相反,当您有希望忽略的新文件时,必须手动编辑和提交文件.gitignore.gitignore 文件包含与存储库中的文件名匹配的模式,以确定是否应忽略它们。

Git 忽略模式

.gitignore使用globbing 模式来匹配文件名。您可以使用各种符号构建您的模式:

模式示例匹配解释
**/logslogs/debug.log
logs/monday/foo.bar
build/logs/debug.log
您可以在模式前面加上双星号以匹配存储库中任何位置的目录。
**/logs/debug.loglogs/debug.log
build/logs/debug.log
但不是logs/build/debug.log
您还可以使用双星号根据文件名和父目录名来匹配文件。
*.logdebug.log
foo.log
.log
logs/debug.log
星号是匹配零个或多个字符的通配符。
*.log
!important.log
debug.log
trace.log
但不是
important.log
logs/important.log
在模式前加上感叹号会否定它。如果一个文件匹配一个模式,但也匹配一个在文件后面定义的否定模式,它不会被忽略。
*.log
!important/.log</br>trace.*
debug.log
important/trace.log
但不是
important/debug.log
在否定模式之后定义的模式将重新忽略任何先前否定的文件。
/debug.logdebug.log
但不是logs/debug.log
前置斜杠仅匹配存储库根目录中的文件。
debug.logdebug.log
logs/debug.log
默认情况下,模式匹配任何目录中的文件
debug?.logdebug0.log
debugg.log
但不是
debug10.log
问号正好匹配一个字符。
debug[0-9].logdebug0.log
debug1.log
但不是
debug10.log
方括号也可用于匹配指定范围内的单个字符。
debug[01].logdebug0.log
debug1.log
但不是
debug2.log
debug01.log
方括号匹配指定集中的单个字符。
debug[!01].logdebug2.log
但不是
debug0.log
debug1.log
debug01.log
感叹号可用于匹配除指定集合中的一个以外的任何字符。
debug[a-z].logdebuga.log
debugb.log
但不是
debug1.log
范围可以是数字或字母。
logslogs
logs/debug.log
logs/latest/foo.bar
build/logs
build/logs/debug.log
如果您不附加斜杠,该模式将匹配具有该名称的文件和目录的内容。在左侧的匹配示例中,忽略名为logs的目录和文件
logs/logs/debug.log
logs/latest/foo.bar
build/logs/foo.bar
build/logs/latest/debug.log
附加斜杠表示该模式是一个目录。存储库中与该名称匹配的任何目录的全部内容(包括其所有文件和子目录)都将被忽略
logs/
!logs/important.log
logs/debug.log
logs/important.log
等一下!logs/important.log不是在左边的例子中被否定啦吗?
由于 Git 中与性能相关的怪癖,您无法否定模式匹配目录中的文件
logs/**/debug.loglogs/debug.log
logs/monday/debug.log
logs/monday/pm/debug.log
双星号匹配零个或多个目录。
logs/*day/debug.loglogs/monday/debug.log
logs/tuesday/debug.log
但不是
logs/latest/debug.log
通配符也可以用在目录名中。
logs/debug.loglogs/debug.log
但不是
debug.log
build/logs/debug.log
指定特定目录中文件的模式是相对于存储库根目录的。(如果你愿意,你可以在前面加一个斜杠,但它没有什么特别的。)

* 这些解释假定您的 .gitignore 文件位于存储库的顶级目录中,这是惯例。如果您的存储库有多个 .gitignore 文件,只需在心里将“存储库根目录”替换为“包含 .gitignore 文件的目录”(并考虑统一它们,以便您的团队保持理智)。

除了这些字符之外,您还可以使用 # 在文件中包含注释.gitignore

# ignore all logs
*.log

.gitignore如果您有包含模式字符的文件或目录,则可以使用 \ 来转义模式字符:

# ignore the file literally named foo[01].txt
foo\[01\].txt

存储库中的共享 .gitignore 文件

Git 忽略规则通常在存储库根目录下的文件中定义。但是,您可以选择在存储库的不同目录中定义多个文件。特定.gitignore文件中的每个模式都相对于包含该文件的目录进行测试。然而,惯例和最简单的方法是.gitignore在根目录中定义一个文件。当您的.gitignore文件被签入时,它会像存储库中的任何其他文件一样进行版本控制,并在您推送时与您的团队成员共享。通常,您应该只包含.gitignore对存储库的其他用户有益的模式。

个人 Git 忽略规则

您还可以在一个特殊文件中为特定存储库定义个人忽略模式,网址为.git/info/exclude. 这些没有版本控制,也没有随您的存储库一起分发,因此它是包含可能只对您有益的模式的合适位置。例如,如果您有自定义日志记录设置,或在您的存储库工作目录中生成文件的特殊开发工具,您可以考虑将它们添加到以防止它们被意外提交.git/info/exclude到您的存储库。

全局 Git 忽略规则

此外,您可以通过设置 Git core.excludesFile属性为本地系统上的所有存储库定义全局 Git 忽略模式。您必须自己创建此文件。如果您不确定将全局文件放在哪里.gitignore,您的主目录是一个不错的选择(并且可以方便以后查找)。创建文件后,您需要使用以下命令配置其位置git config

$ touch ~/.gitignore
$ git config --global core.excludesFile ~/.gitignore

您应该小心选择全局忽略哪些模式,因为不同的文件类型与不同的项目相关。特殊操作系统文件(例如.DS_Storethumbs.db)或某些开发人员工具创建的临时文件是全局忽略的典型候选者。

忽略以前提交的文件

如果您想忽略过去提交的文件,您需要从存储库中删除该文件,然后.gitignore为其添加规则。使用--cached选项意味着该文件将从您的存储库中删除,但将作为被忽略的文件保留在您的工作目录中。

$ echo debug.log >> .gitignore
  
$ git rm --cached debug.log
rm 'debug.log'
  
$ git commit -m "Start ignoring debug.log"

--cached如果要从存储库和本地文件系统中删除文件,则可以省略该选项。

提交一个被忽略的文件

可以使用-f(或--force)选项强制将忽略的文件提交到存储库:

$ cat .gitignore
*.log
  
$ git add -f debug.log
  
$ git commit -m "Force adding debug.log"

如果您定义了一个通用模式(如 ),但您想要提交一个特定文件,您可能会考虑这样做。然而,更好的解决方案是定义一般规则的例外:*.log

$ echo !debug.log >> .gitignore
  
$ cat .gitignore
*.log
!debug.log
  
$ git add debug.log
  
$ git commit -m "Adding debug.log"

对于您的队友来说,这种方法更明显,也更不容易混淆。

隐藏一个被忽略的文件

git stash是一个强大的 Git 功能,用于暂时搁置和恢复本地更改,允许您稍后重新应用它们。如您所料,默认情况下git stash会忽略被忽略的文件,并且只会隐藏对 Git 跟踪的文件的更改。但是,您也可以使用--all 选项调用 git stash以将更改存储到被忽略和未跟踪的文件中。

调试 .gitignore 文件

如果您有复杂的.gitignore模式,或者模式分布在多个.gitignore文件中,则可能很难找出特定文件被忽略的原因。您可以使用git check-ignore带有-v(or --verbose) 选项的命令来确定导致特定文件被忽略的模式:

$ git check-ignore -v debug.log
.gitignore:3:*.log  debug.log

输出显示:

<file containing the pattern> : <line number of the pattern> : <pattern>    <file name>

git check-ignore如果愿意,您可以将多个文件名传递给,这些名称本身甚至不必与存储库中存在的文件相对应。


harriszh
341 声望131 粉丝

做些有趣的事,留些有用的存在