概述
Git中的文件主要有三种状态。
-
被追踪状态
文件被暂存或者已经提交
-
未被追踪状态
文件未被暂存或者提交
-
被忽略态
Git已经显示规定要被忽略的文件
被忽略的文件通常是通过仓库源代码人工构建的制品和机器自动产生的文件。它们不应该被提交到仓库中。一些常见的例子如下
- 依赖的缓存文件。如 /node_modules、/packages下的文件
- 被编译的代码。如.o 、.pyc和.class文件
- 构建输出目录。如/bin、/out、 或者 /target
- 运行时产生的文件。如.log, .lock, 或 .tmp文件
- 被隐藏的系统文件。如.DS_Store 或 Thumbs.db
- 个人的IDE配置文件。如.idea/workspace.xml
仓库的根目录下有一个叫==.gitignore==文件,所有的被忽略的文件都可以在次文件中进行定义。当你想要手动的忽略一些文件时,没有命令可以使用,你只能手动创建.gitignore文件并编辑它。.gitignore文件中包含可以匹配你的仓库的所有的文件的模式,以此来决定哪些文件将会被忽略。
Git中忽略文件主要有以下方式。
- 模式匹配
- 共享.gitignore文件
- 定制化.gitignore规则
- 全局.gitignoire规则
- 从已经提交的的文件中忽略
- 提交一个被忽略的文件
- 暂存一个被忽略的文件
- 调试一个被忽略的文件
模式匹配
.gitignore使用通配符去匹配所有的文件,你可以使用各种不同的符号去构建你的模式。
模式 | 匹配的例子 | 解释 |
---|---|---|
**/logs | logs/debug.log logs/monday/foo.bar build/logs/debug.log | 匹配路径中所有带有logs的文件 |
**/logs/debug.log | logs/debug.log build/logs/debug.log 不能匹配logs/build/debug.log | 匹配路径中含有/logs/debugs的文件 |
*.log | debug.log foo.log .log logs/debug.log | *匹配0个或者多个字符 |
*.log !important.log | debug.log trace.log 不能匹配important.log logs/important.log | !来表示非,定义反模式。如果一个文件先被一个模式匹配,又被一个反模式匹配,那么不会匹配到这个文件。如logs/important.log 同时被*.logs和!important匹配到,所以不会匹配到 |
*.log !important/*.log trace.* | debug.log important/trace.log不能匹配important/debug.log | important/trace.log先被匹配到,后被取消,最后又匹配到,所以可以匹配到 |
/debug.log | debug.log不能匹配logs/debug.log | /表示从仓库的根目录开始匹配 |
debug.log | debug.log logs/debug.log | 默认一个文件会匹配所有目录 |
debug?.log | debug0.log debugg.log` 不能匹配debug10.log | ?只能匹配一个字符 |
debug[0-9].log | debug0.log debug1.log 不能匹配 debug10.log | [a-b]匹配一个范围内的单个字符 |
debug[01].log | debug0.log debug1.log而不能匹配debug2.log debug01.log | [ab]匹配a或者b, 不能匹配多个字符 |
debug[!01].log | debug2.log不能匹配debug0.log debug1.log debug01.log | [!01]匹配除了0,1以外的一个字符 |
logs | logs 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/ !logs/important.log | logs/debug.log logs/important.log | 注意,被目录匹配到的不能由反模式取消,如本例中的logs/important.log |
logs/**/debug.log | logs/debug.log logs/monday/debug.log logs/monday/pm/debug.log | **匹配0个或多个目录 |
logs/*day/debug.log | logs/monday/debug.log logs/tuesday/debug.log 不能匹配logs/latest/debug.log | *一样可以用在目录匹配中,表示0个或者多个字符 |
logs/debug.log | logs/debug.log不能匹配debug.log build/logs/debug.log | 指定logs是相对于仓库根目录的,你也可以写成/logs/debug.log |
以上规则是建立在你的.gitignore在仓库的根目录的前提下的。
# 进行注释, \进行转义操作
# ignore the file literally named foo[01].txt
foo\[01\].txt
我常常写java, 所以我给出一个springboot项目中常见的.gitignore文件, 其他的自己配置即可
# target
target/
*.class
# IntelliJ IDEA
.idea/
# log
*.log
共享.gitignore文件
通常你可以在仓库的根目录下的.gitignore文件中定义你个规则,你也可以在仓库的不同的目录下定义多个.gitignore文件。每个.gitignore文件只是会对所在目录的文件有效。
定制化.gitignore规则
你可以在.git/info/exclude下为某个特别的仓库定制特定的规则。很适合包含那些只是对你个人有利的规则。例如,你有一个自定义的日志的记录工具的设置,或者某个将会在你的仓库里产生文件的开发工具,你可以考虑将他们加到.git/info/exclude中,防止他们产生的文件自动提交到仓库中。
全局.gitignoire规则
你可以通过设置core.excludesFile 属性来为所有的仓库设置一个全局的忽略规则。首先在你能够轻松找到的位置创建.gitignore文件,这里我们在家目录创建。然后设置core.excludesFile属性。
touch ~/.gitignore
git config --global core.excludesFile ~/.gitignore
通过来说,一些操作系统产生的临时文件或者某个开发工具产生的文件应该被全局忽略。
从已经提交的的文件中忽略
如果你想要从已经提交的仓库中忽略某些文件,你可以删除这个文件,然后写入.gitignore规则。如果你想保留这个文件,你可以使用git rm -cache选项。例如
echo debug.log >> .gitignore # 写入规则
$ git rm --cached debug.log # 从仓库中删除,但是本地仍然保留
rm 'debug.log'
$ git commit -m "Start ignoring debug.log"
如果你想要从本地和仓库中都删除,忽略–cached选项
提交一个被忽略的文件
git add命令的 -f(或者–force) 选项可以强制提交被忽略的文件
cat .gitignore
*.log
$ git add -f debug.log
$ git commit -m "Force adding debug.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追踪的文件有效,而对被忽略的文件是无效的。使用git stash --all option 暂存所有的未被追踪的文件。git stash pop 则可以恢复本地的修改。
调试一个被忽略的文件
如果你的文件规则很复杂, 你可以使用git check-ignore 来搞清楚某个文件是否会被忽略。你甚至可以传递多个文件,无论这个文件在仓库里是否存在。
git check-ignore -v Image/6CC27F04A7E4BD2122F3D04C2B9F9E3E
输出: .gitignore:12:Image/ Image/6CC27F04A7E4BD2122F3D04C2B9F9E3E