使用 .gitignore 文件忽略文件和目录

你可以通过在存储库中创建一个或多个 .gitignore 文件,使 Git 忽略某些文件和目录 - 也就是说,将它们排除在 Git 之外

在软件项目中,.gitignore 通常包含在构建过​​程中或在运行时生成的文件和/或目录的列表。.gitignore 文件中的条目可能包含指向以下内容的名称或路径:

  1. 临时资源,例如缓存,日志文件,编译代码等。
  2. 不应与其他开发人员共享的本地配置文件
  3. 包含机密信息的文件,例如登录密码,密钥和凭据

在顶级目录中创建时,规则将递归应用于整个存储库中的所有文件和子目录。在子目录中创建时,规则将应用于该特定目录及其子目录。

忽略文件或目录时,它不会是:

  1. 由 Git 跟踪
  2. git statusgit diff 等命令报告
  3. 上演了 git add -A 等命令

在你需要忽略跟踪文件的特殊情况下,应特别小心。请参阅: 忽略已提交到 Git 存储库的文件

例子

以下是 .gitignore 文件中基于 glob 文件模式的一些通用规则示例 :

# Lines starting with `#` are comments.

# Ignore files called 'file.ext'
file.ext

# Comments can't be on the same line as rules!
# The following line ignores files called 'file.ext # not a comment'
file.ext # not a comment 

# Ignoring files with full path.
# This matches files in the root directory and subdirectories too.
# i.e. otherfile.ext will be ignored anywhere on the tree.
dir/otherdir/file.ext
otherfile.ext

# Ignoring directories
# Both the directory itself and its contents will be ignored.
bin/
gen/

# Glob pattern can also be used here to ignore paths with certain characters.
# For example, the below rule will match both build/ and Build/
[bB]uild/

# Without the trailing slash, the rule will match a file and/or
# a directory, so the following would ignore both a file named `gen`
# and a directory named `gen`, as well as any contents of that directory
bin
gen

# Ignoring files by extension
# All files with these extensions will be ignored in
# this directory and all its sub-directories.
*.apk
*.class

# It's possible to combine both forms to ignore files with certain
# extensions in certain directories. The following rules would be
# redundant with generic rules defined above.
java/*.apk
gen/*.class

# To ignore files only at the top level directory, but not in its
# subdirectories, prefix the rule with a `/`
/*.apk
/*.class

# To ignore any directories named DirectoryA 
# in any depth use ** before DirectoryA
# Do not forget the last /, 
# Otherwise it will ignore all files named DirectoryA, rather than directories
**/DirectoryA/
# This would ignore 
# DirectoryA/
# DirectoryB/DirectoryA/ 
# DirectoryC/DirectoryB/DirectoryA/
# It would not ignore a file named DirectoryA, at any level

# To ignore any directory named DirectoryB within a 
# directory named DirectoryA with any number of 
# directories in between, use ** between the directories
DirectoryA/**/DirectoryB/
# This would ignore 
# DirectoryA/DirectoryB/ 
# DirectoryA/DirectoryQ/DirectoryB/ 
# DirectoryA/DirectoryQ/DirectoryW/DirectoryB/

# To ignore a set of files, wildcards can be used, as can be seen above.
# A sole '*' will ignore everything in your folder, including your .gitignore file.
# To exclude specific files when using wildcards, negate them.
# So they are excluded from the ignore list:
!.gitignore 

# Use the backslash as escape character to ignore files with a hash (#)
# (supported since 1.6.2.1)
\#*#

大多数 .gitignore 文件是各种语言的标准文件,所以要开始使用,这里有一组样本 .gitignore 文件, 按语言列出,可以从中克隆或复制/修改到项目中。或者,对于新项目,你可以考虑使用在线工具自动生成启动文件。

其他形式的 .gitignore

.gitignore 文件旨在作为存储库的一部分提交。如果要在不提交忽略规则的情况下忽略某些文件,可以使用以下选项:

  • 编辑 .git/info/exclude 文件(使用与 .gitignore 相同的语法)。规则将在存储库的范围内是全局的;
  • 设置一个全局 gitignore 文件 ,该文件将忽略规则应用于所有本地存储库:

此外,你可以忽略对跟踪文件的本地更改,而无需更改全局 git 配置:

  • git update-index --skip-worktree [<file>...]:用于轻微的局部修改
  • git update-index --assume-unchanged [<file>...]:用于生产就绪,上游不变的文件

有关更多选项,请参阅后面标志git update-index 文档 之间差异的更多详细信息

清理被忽略的文件

你可以使用 git clean -X 清除被忽略的文件:

git clean -Xn #display a list of ignored files
git clean -Xf #remove the previously displayed files

注意:-X(caps) 清除被忽略的文件。使用 -x(无大写字母)也可以删除未跟踪的文件。

有关更多详细信息,请参阅 git clean 文档

有关详细信息,请参阅 Git 手册