忽略 git 存储库中跟踪文件的更改

Ignore changes in tracked files in git repository

可能是重复的,但还没有找到这个问题的答案。

我们有一个存储库,该存储库包含一些跟踪文件,例如“.gitignore”文件,以及 "conf" 文件夹。

我不想跟踪此文件和文件夹,因为它包含只能在我的机器上运行的更改,并且我不想通过提交对这些配置文件的更改来破坏其他人的设置。

在“.gitignore”中添加此文件和文件夹没有帮助,因为它仍在被跟踪。

如何避免跟踪这些文件中的这些更改?

你想要的是设置标志assume-unchanged。您可以为此目的使用 git update-index

git update-index --assume-unchanged .gitignore

稍后,如果您想取消设置标志以提交对 fil 的更改,请使用

git update-index --no-assume-unchanged .gitignore

更多信息,请参考git doc:

--[no-]assume-unchanged

When this flag is specified, the object names recorded for the paths are not updated. Instead, this option sets/unsets the "assume unchanged" bit for the paths. When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

编辑

要在文件夹上应用标志,您需要对文件夹中的每个文件执行命令。有关自动执行此任务的提示,请参阅 this answer(如果您的文件夹包含许多文件)。 示例:

cd /path/to/the/folder
git ls-files -z | xargs -0 git update-index --assume-unchanged