Home > Software engineering >  How to tell Git to ignore what has been changed in a file since the last staged/commit?
How to tell Git to ignore what has been changed in a file since the last staged/commit?

Time:04-30

I do not want to undo the changes. I do not want Git to ignore this file.

(Note: a totally hypothetical situation in order to explain the question.)

I'm an inexperienced programmer and I have hardcoded a date that is used to calculate the price of whatever. So today I changed that date, ran the code, and of course, Git tracked the change. I want to see that date the next time I run this code so I do not want to undo the change.

How can I tell Git to ignore that change as there is no point in staging/committing etc?

Sure, if I make other code changes then I do want Git to track.

In other words, how to tell Git that the changes that I have made since the last stage/commit are to be ignored/forget it/you didn't see it?

CodePudding user response:

There is no way to ignore tracked files in Git. The Git FAQ is very clear about this. It specifically says:

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.

What the Git FAQ recommends for this case is to store the committed portion in a file in a different location, and then by default use a script to copy that value to its intended location, which is ignored. You can then modify the ignored file to have whatever value you like without needing to ignore it.

  •  Tags:  
  • git
  • Related