r/git • u/noobdainsane • Oct 17 '26
When to create a new commit vs interactive rebase?
An old commit has a bug. Should I create a new commit addressing the fix, or fix that bug in-place with a rebase? For example the bug is that I forgot to gitignore a file and it is being modified at runtime, generating git diffs. The fix is not just adding it to gitignore, but actually completely re-generating my .gitignore file by another tool which I just learned instead of what the default Github CLI generates.
Should I rebase the new gitignore into literally the original initial commit or create a new commit addressing the new gitignore? But this way the old commit will still have the bug.
Should you care keeping each commit clean? Or have a new commit fix it? I know people generally use the later, but at least for a small project like mine, rebasing doesn't seem to harm.
I think there is also some consideration of if multiple people have pulled the history you are about to change. I don't know, but I for this project it is mostly only me who is working on it.
15
u/rosentmoh Oct 17 '26
Simple rule: if you already pushed the faulty commit and others can see it then only a new commit fixing the bug is the sane option. If instead this faulty commit is still only local to you, then you can fixup that commit via rebase or whatever before pushing.
5
u/ivancea Oct 17 '26
Why would you want to rewrite a commit instead of just adding a new one?
4
u/Charming-Designer944 Oct 17 '26
If pushed to a shared repository then a new commit.
If part of your working tree for something not yet shared, then a new commit and before pusing/sharing then do an interactive rebaae to both bng it up to date and clean up the (still local) history to something presentable.
Commit locally often. Every time you switch line of thought or at least once per hour. This way it's easy to clean things up as your individual commits are small and isolated and can easily be represented and squashed into larger focused commits without having to split split or edit any individual commit.
2
u/Broad-Promise6954 ancient Oct 18 '26
There are several "schools of thought" on whether one should rewrite commits. I don't fully buy into any of them.
There's one thing to know first though: you can't actually change any commit, at all, ever. What you can do instead is make a new and improved, different commit, and then have yourself and everyone else use that new-and-improved commit instead of the original. Once you understand this, most of the rest just falls into place.
[Side note: this means
git commit --amendis a lie. When used correctly, it's a little white lie, which is fine. It works by having Git boot out the previous end-of-the-branch commit, using your new-and-improved commit instead. Nobody knows or cares except you!]In particular, there's a key implication of this "can't change any existing commit at all" law: if nobody else has some commits you've made, those other people will never know if you abandon those commits in favor of some new and improved commits instead.
So now, keeping the above in mind, let's get to your case. You have a series of commits, and there's something bad in an earlier one. Let's draw a chain of those commits to help visualize them:
Your branch name
your-branchnames commitC4, which is the end of the chain. CommitC2has an error in it, and commitC3namesC2(and contains the same error) andC4namesC3(and continues to propagate the error).You can make a new commit C5, which you add to the end of your branch, that corrects the error, like this:
The name
your-branchnow refers to the corrected commitC5, which refers back to the "bad" commitC4, etc.If you use
git rebase --interactive, you can choose to rebase the whole chain starting fromC2onward ontoC1, while making adjustments. The adjustment to make is to squash the fix inC5into what you did inC2that was wrong. The new chain won't do anything to the old chain. It literally can't. But you'll just abandon the old chain as useless. Let's draw it kicked up out of the way, like this:When you have Git display the chain of commits, starting at
C8and working backwards, you never see the old "bad" commits. They are just gone, as far as you're concerned. And as long as nobody else has the old commits, no one can ever know that you did this.But what happens if someone else does have your old commits? Well, now you have a problem that's easily solved: you just have to convince them, whoever they are, to do exactly what you did, abandon the old ones in favor of the new-and-improved ones.
This is where "team policy" enters the picture. If your team policy is "you can't do that", well, now you can't do that. If it's "everyone on the team must do that", you can do that, they have to follow along. If there's no policy, you're in some grey area and you'll have to muddle through.