0

I want this change to never be committed, not even in future.

I know this is kinda possible using sourcetree. People always say git commands can do everything git GUI can do, and more. But in this case I feel like maybe the GUI is more powerful.

Tried searching stack overflow but found only results about not stashing a particular file.

  • 3
    What’s the change? If it’s a whole file then you can ignore it with `.gitignore`. – Guildenstern Apr 17 '23 at 21:06
  • 1
    ...but only if that file has never been committed before, because `.gitignore` is not retroactive. (It's `.gitignore`, not `.gitforget`.) – Jim Redmond Apr 17 '23 at 21:13
  • if you don't want to commit a change, then you should untracked it. maybe you wan to avoid using git add . or just ignore it with `.gitignore` as suggested – Hassan Liasu Apr 17 '23 at 21:29
  • 1
    _"But in this case I feel like maybe the GUI is more powerful."_ -- Most Git GUIs (SourceTree included) build and run Git command lines behind the scenes. They cannot be more powerful than the Git command line; they are just easier to use. – axiac Apr 17 '23 at 21:34
  • Would [this suggestion](https://stackoverflow.com/a/46399763/86072) work for you ? – LeGEC Apr 18 '23 at 03:55
  • The stuff I don't want to commit is a line in a configuration file and a paragraph in pom.xml, @Guildenstern. It seems like I will have to gitignore it. Jim, that's a great point I should keep in mind. – Bhavika Khare Apr 18 '23 at 15:35
  • @axiac, then how does sourcetree manage to let me leave a specific line out of my commit? – Bhavika Khare Apr 18 '23 at 15:39
  • @LeGEC, no, then I'd have to keep adding the change back in after I remove it just while committing. I want the configuration change to stay in my local but want to be able to opt out of staging it each time. – Bhavika Khare Apr 18 '23 at 15:40
  • 1
    @BhavikaKhare: you can use `git add -p` to stage only parts of a file, or `git gui` to stage/unstage only selected lines – LeGEC Apr 18 '23 at 17:30
  • 1
    _"I want this change to never be committed, not even in future."_ -- sounds like you need to extract that part of the code (or is it a configuration?) into a separate file. Then you add the new file to `.gitignore` and put in Git a default version of the file as example (with a different name, of course). – axiac Apr 18 '23 at 18:02
  • `I want this change to never be committed, not even in future.` Yes, you do. You want to check in such changes as [temporary commits](https://stackoverflow.com/a/75010635/23118). – hlovdal Apr 19 '23 at 23:56

1 Answers1

2

The best way to do this is to remove that file from the history with git rm --cached FILE and then add it to .gitignore. Then, the file will not be tracked and won't be modified in the future, because it won't be in the repository.

If your question is how to keep the file tracked and ignore changes to a tracked file, the Git FAQ is clear that that's not possible:

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.

Note that the FAQ has a recommendation for the common case of configuration files:

If your goal is to modify a configuration file, it can often be helpful to have a file checked into the repository which is a template or set of defaults which can then be copied alongside and modified as appropriate. This second, modified file is usually ignored to prevent accidentally committing it.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • Where can I find more information on the FAQ recommendation? What do they mean by copied alongside? – Bhavika Khare Apr 18 '23 at 15:43
  • 1
    e.g: put `myconfig` in `.gitignore`, commit a `myconfig.template` file with a sample configuration (or indications of the parameters to set), and instruct developers to copy `myconfig.template` as `myconfig`, and set the settings there – LeGEC Apr 18 '23 at 17:31