Make .gitignore ignore everything except a few files

Make .gitignore ignore everything except a few files

技术背景

在使用Git进行项目管理时,.gitignore文件用于指定哪些文件或目录应该被Git忽略,不纳入版本控制。有时候,我们希望忽略项目中的大部分文件,只包含少数特定的文件,这就需要正确配置.gitignore文件。

实现步骤

忽略所有文件,仅包含特定文件

若要忽略所有文件,但包含特定文件,可在.gitignore文件中按如下设置:

1
2
3
4
5
6
7
8
9
10
11
# Ignore everything
*

# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...

# ...even if they are in subdirectories
!*/

忽略目录下的所有内容,仅包含其中一个文件

若要忽略目录下的所有内容,仅包含其中一个文件,需为文件路径中的每个目录编写一对规则。例如,忽略pippo文件夹中的所有内容,但包含pippo/pluto/paperino.xml

1
2
3
4
pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml

忽略特定目录下的所有文件,仅包含某些子目录和文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Blocklist files/folders in same directory as the .gitignore file
/*

# Includelist some files
!.gitignore
!README.md

# Ignore all files named .DS_Store or ending with .log
**/.DS_Store
**/*.log

# Includelist folder/a/b1/ and folder/a/b2/
# trailing "/" is optional for folders, may match file though.
# "/" is NOT optional when followed by a *
!folder/
folder/*
!folder/a/
folder/a/*
!folder/a/b1/
!folder/a/b2/
!folder/a/file.txt

核心代码

忽略所有文件,包含特定文件

1
2
3
*
!.gitignore
!someFile.txt

忽略嵌套目录下的所有文件,包含特定文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Skip all files
*

# But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

工具生成规则

可使用 git-do-not-ignore 工具生成规则。示例输入:

1
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

示例输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

最佳实践

正确使用/*

多数情况下,应使用/*而非**/。使用*会递归匹配,可能无法按预期工作。例如:

1
2
3
4
5
6
7
# 错误示例
folder
!folder/some-file.txt

# 正确示例
folder/*
!folder/some-file.txt

注意规则顺序

在忽略和包含文件时,规则顺序很重要。例如,忽略application文件夹中的所有内容,但包含index.phpconfig文件夹:

1
2
3
4
5
6
7
8
9
# 错误示例
application/*
!application/config/*
!application/index.php

# 正确示例
!application/config/*
!application/index.php
application/*

强制添加文件

若想包含被.gitignore忽略的文件,可使用git add -f命令。例如:

1
2
# x64文件夹被忽略,但包含x64/Release/myFile.py
git add -f x64/Release/myFile.py

常见问题

无法重新包含被忽略的文件

若父目录被忽略,就无法重新包含该目录下的文件。例如:

1
2
3
4
5
6
7
8
# 错误示例
*
!bin/script.sh

# 正确示例
*
!bin
!bin/script.sh

嵌套目录规则编写困难

手动编写嵌套目录的规则较为困难,可使用 git-do-not-ignore 工具生成规则。

子文件夹忽略问题

若要忽略文件夹中的子文件夹,需确保路径中包含*。例如:

1
2
3
4
5
6
7
# 错误示例
/folder/
!/folder/subfolder

# 正确示例
/folder/*
!/folder/subfolder

Make .gitignore ignore everything except a few files
https://119291.xyz/posts/2025-05-13.make-gitignore-ignore-everything-except-few-files/
作者
ww
发布于
2025年5月13日
许可协议