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

标签 git batch-file

我是 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/

相关文章:

batch-file - 如何将 REG 值读入环境变量

git - 跟踪文件但从 git 包中排除它们

windows - 如何使用 powershell 将反斜杠 CR LF 替换为 <br>

batch-file - 批处理日期时间戳比较

powershell - 如何正确显示气球提示?

powershell - Powershell中批处理文件中的错误识别

git - 如何在保持子分支完整的情况下 merge 父分支

git - 刚开始使用 TortoiseGit,为什么本地存储库需要我的电子邮件?

windows - 无法在具有自签名证书的 Windows 上使用 git 解析 "unable to get local issuer certificate"

git - 如何从 git 跟踪中删除文件?