Git diff不会忽略.gitignore中的指定文件

时间:2013-07-23 20:07:25

标签: git batch-file

我是git的新手,我不知道我做错了什么。我想跑     git diff --name-only 但是我想排除以.test结尾的文件,这意味着即使这些已经改变了,我也不希望git diff输出它已经改变了。 我试图将这些文件添加到.gitignore文件中,但是当我运行git diff命令时,所有这些都包括.gitignore!

我做错了什么以及如何在运行git diff时排除文件?

1 个答案:

答案 0 :(得分:16)

好的,这就是你做错了。 Git会继续跟踪这些文件,因为您已经开始在项目的早期跟踪它们。在某些时候,您之前运行的初始提交看起来像是:

git add . # this added everything to the tracking system including your .test files
git commit -a -m 'I just committed everything in my project .test files included'

放置东西是你的gitignore文件将保留你创建的以.test结尾的未来文件进入你的项目,但是你需要删除已经告诉git跟踪git内存的.test文件。将内容放入gitignore不会对已经跟踪的文件执行任何操作。你现在需要做的是:

选项1:

# you can remove the files from gits tracking system and then put them back
# when you go to put them back now git will have no memory of tracking these
# and will consider future edits to be ignored
# Back your .test files up before doing this, and this will delete them from your project
git rm /path/to/your/file.test

选项2:

# this is safer but does not use gitignore at all
git update--index --assume-unchanged /path/to/your/file.test

当你运行选项2时,你告诉git,其余时间该文件永远不会改变(即使它在现实生活中也是如此)这可以让你将.test文件保存为被跟​​踪项目的一部分(因为它们是现在),但是git永远不会再打扰你对它们的改变。请注意,此操作可以随时撤消,并且不具有破坏性,这就是为什么它更安全。你也应该在使用之前先阅读它。

https://www.kernel.org/pub/software/scm/git/docs/git-update-index.html

相关问题