在Git工作区的根目录下创建一个特殊文件.gitignore,然后把要忽略的文件名填进去,Git就会自动忽略这些文件。
不需要从头写.gitignore文件,GitHub已经为我们准备了各种配置文件,只需要组合一下就可以使用了。所有配置文件可以直接在线浏览:https://github.com/github/gitignore
# .gitignore 示例
/node_modules
.env
*.log
最后一步就是把.gitignore也提交到Git,就可以了!当然检验.gitignore可以用git status。
有些时候,你想添加一个文件到Git,但发现添加不了,原因是这个文件被.gitignore忽略了:
git add App.class
The following paths are ignored by one of your .gitignore files:Download.class Use -f if you really want to add them.
如果你确实想添加该文件,可以用-f强制添加到Git:
git add -f Download.class
或者你发现,可能是.gitignore写的有问题,需要找出哪个规则写错了,可以用git check-ignore命令检查:
git check-ignore -v Download.class
.gitignore:3:*.class Download.class
Git会告诉我们,.gitignore的第3行规则忽略了该文件。