我是 git 的新手,我不知道自己做错了什么。我想跑 git diff --name-only 但我想排除以 .test 结尾的文件,这意味着即使这些文件已经更改,我也不希望 git diff 输出它已更改。 我试图将这些文件添加到 .gitignore 文件中,但所做的只是在我运行 git diff 命令时包含 .gitignore!
我做错了什么以及如何在运行 git diff 时排除文件?
最佳答案
好吧,这就是你做错的地方。 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 结尾的 future 文件进入你的项目,但是你需要从 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
关于Git diff 不会忽略 .gitignore 中的指定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17820056/