git不会忽略2个特别命名的文件

eri*_*ork 60 git gitignore

我想自动从提交中排除2个文件,这些文件是git存储库的一部分但有一些更改,它们不应该是github repo的一部分,因为它们有一些只对我的本地系统有意义的更改,而不是另一个开发人员.

我把它们添加到.git/info/exclude希望git了解他不应该对它们进行更改:

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
a.conf
b.conf
# *~
Run Code Online (Sandbox Code Playgroud)

git status仍然将它们列为提交的阶段:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   build/conf/a.conf
#   modified:   build/conf/b.conf
#
Run Code Online (Sandbox Code Playgroud)

我不想在每次提交时定期取消它们(有一次我可能会忘记),我该怎么做?

eck*_*kes 140

您希望忽略对跟踪文件的更改.这无法实现(正如Charles Bailey 所说),无论是用.gitignore还是用.git/info/exclude.

你需要使用git update-index:

git update-index --assume-unchanged build/conf/a.conf
git update-index --assume-unchanged build/conf/b.conf
Run Code Online (Sandbox Code Playgroud)

将实现您想要的:文件始终保持不变.

如果要再次跟踪这些文件中的更改,请使用--no-assume-unchanged.

最后,如果您想知道哪些文件当前处于--assume-unchanged模式中,请询问git for

git ls-files -v | grep -e "^[hsmrck]"
Run Code Online (Sandbox Code Playgroud)

  • @eckes 无论如何要让这个工作适用于所有分支机构?如果我使用该命令进行更改,我无法切换分支并给我提交或存储它们的错误 (2认同)