【问题标题】:Git diff doesn't ignore specified files in .gitignoreGit diff 不会忽略 .gitignore 中的指定文件
【发布时间】:2013-07-23 02:31:33
【问题描述】:

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

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

【问题讨论】:

  • 显示 .gitignore 文件的内容
  • 另外你可能在你的 gitignore 文件中犯了一个错误,我们可以在你发布它之后看看你是否这样做了。

标签: git batch-file


【解决方案1】:

好吧,这就是你做错了。 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

【讨论】:

  • 对于 git 1.8,命令将是: git update-index --assume-unchanged /path/to/your/file.test 顺便说一句,很好的答案,它对我帮助很大。跨度>
  • 没问题,很高兴能帮上忙。
  • 使用 git rm --cached 只会从索引中删除文件 - 无需事先备份。
  • update--index 中的小错字。它应该只有一个-
  • @regina_fallangi 已修复!
猜你喜欢
  • 2011-03-18
  • 2012-03-15
  • 2018-05-02
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多