0

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.

AdmiralOrange
  • 157
  • 1
  • 14

1 Answers1

0

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.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • Thanks for your advice of committing it in a different location and using a script to manage. It is a Unity Engine project, I don't know how to do this yet. – AdmiralOrange May 05 '22 at 07:52
  • According to "Git FAQ says this": it doesn’t know whether the changes should be kept, or whether they safely be destroyed. In my situation, I know, so I think I may use Git Hook to do this, are you familar with Git Hook, do you know if Git Hook can do this job? – AdmiralOrange May 05 '22 at 07:55
  • Maybe I should go for a Git Hook tutorial. Maybe I cannot avoid this step by just asking a question and expecting a good answer. – AdmiralOrange May 05 '22 at 08:36
  • Git hooks cannot ignore changes to tracked files. There is no functionality to do that, using hooks or otherwise. – bk2204 May 06 '22 at 11:47
  • https://stackoverflow.com/a/59484661/2530737, it seems using .gitattributes(a content filter driver) is an option. – AdmiralOrange May 23 '22 at 09:50