0

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?

ThomasAJ
  • 723
  • 2
  • 8
  • 17
  • only solution is `.gitignore` or else every push remove this line of code and add after push complete – Bhargav Chudasama Apr 29 '22 at 09:30
  • Try `git update-index --skip-worktree $path` or `git update-index --assume-unchanged $path`, and use `--no-skip-worktree` and `--no-assume-unchanged` to cancel the effect. As to the differences between the 2 flags, see https://stackoverflow.com/questions/13630849/git-difference-between-assume-unchanged-and-skip-worktree. – ElpieKay Apr 29 '22 at 10:51
  • 1
    Does this answer your question? [Can I 'git commit' a file and ignore its content changes?](https://stackoverflow.com/questions/3319479/can-i-git-commit-a-file-and-ignore-its-content-changes) – 1615903 Apr 29 '22 at 11:35
  • One option for handling local debug code changes like this is to stick them on a branch. Whenever you want to test a feature, rebase your test branch onto your feature branch. When done testing, push your feature branch but *not* your test branch. (Though worth noting this is a code smell - you generally should not need to make code changes to test your code. Local changes mean that your tests may not reflect reality, which can result in bugs slipping through your testing.) – 0x5453 Apr 29 '22 at 20:42

1 Answers1

1

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.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • Thanks @bk2204 and thanks to everyone else. Being a beginner Gitian, I'll stick to the straight and narrow for now, even though the workarounds might work for more experienced practitioners. – ThomasAJ Apr 30 '22 at 03:26