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

use*_*585 10 git batch-file

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

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

usu*_*oio 18

好的,这就是你做错了.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'
Run Code Online (Sandbox Code Playgroud)

把东西放在你的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
Run Code Online (Sandbox Code Playgroud)

选项2:

# this is safer but does not use gitignore at all
git update--index --assume-unchanged /path/to/your/file.test
Run Code Online (Sandbox Code Playgroud)

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

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

  • 使用`git rm --cached`只会从索引中删除文件 - 无需事先备份. (5认同)
  • 对于git 1.8,命令将是:git update-index --assume-unchanged /path/to/your/file.test顺便说一下,很好的答案,它对我帮助很大. (2认同)