简体   繁体   中英

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

I have files tracked in git, I want to ignore changes to some of them.

"git update-index --skip-worktree" and "git update-index --assume-unchanged" don't meet my demands. If there are changes on remote, "git pull" would raise conflicts.
I would like a way which "git pull" goes with no conflicts (If a file is changed both in local repository and upstream, using either local or remote version is OK for me).

Another problem for "--skip-worktree" and "--assume-unchanged" is that, if I delete the file, It shows no change.
I want to ignore changes, but adding or deleting should not be ignored.

I have learned that "Git Hook" can do customizable behaviors.
But I'm not sure if "Git Hook" can do the job. And the document is not straightforward enough for me to know how to accomplish my demands.
So I'm asking a question here.

There is no way to ignore changes to tracked files. The 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.

If you have a file that you'd like to have modified, instead of checking in that file in its normal location, commit it in a different location as an example or template file, and then copy the script into its intended and ignored location with a script (templating it along the way if necessary). You can then keep the intended file in its location without worrying about Git modifying it or caring about it, since it will be ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM