Git: 如何使用 Git Hook 忽略对某些跟踪文件的更改?

Git: How to ignore changes to some tracked files with Git Hook?

我在 git 中跟踪了文件,我想忽略对其中一些文件的更改。

"git update-index --skip-worktree" 和 "git update-index --assume-unchanged" 不符合我的要求。如果远程有变化,“git pull”会引发冲突。
我想要一种“git pull”没有冲突的方式(如果文件在本地存储库和上游都被更改,使用本地或远程版本对我来说都可以)。

“--skip-worktree”和“--assume-unchanged”的另一个问题是,如果我删除文件,它不会显示任何变化。
我想忽略修改,但增删不应该被忽略

我了解到“Git Hook”可以进行自定义行为。
但我不确定“Git Hook”是否可以完成这项工作。而且文档不够直接让我知道如何完成我的需求。
所以我在这里问一个问题。

无法忽略对跟踪文件的更改。 Git FAQ says this:

Git doesn’t provide a way to do this. The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn’t know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed. Therefore, it has to take the safe route and always preserve them.

It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

如果您有一个想要修改的文件,与其在正常位置签入该文件,不如将其作为示例或模板文件提交到其他位置,然后将脚本复制到其使用脚本指定和忽略的位置(如有必要,请沿途对其进行模板化)。然后您可以将目标文件保留在其位置,而不必担心 Git 修改它或关心它,因为它将被忽略。