使用 .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 手冊