You finally found it: a bug in an old commit! And luckily, you already have a solution in mind. So you just need to fix your mistakes in a new commit and everything is fine, right?

Image for post

Photo by the author.

Well, not really. If you follow this strategy for a while, your commit history will be littered with these little “band-aid” commits. They don’t have any semantic meaning — and they shouldn’t be there in the first place! When someone else (or you, after some time) takes a look at the commit history, they will see the flawed original commit, scratch their heads, and only later, sprinkled somewhere in the commit log, find a commit that corrects the original commit’s mistakes.

Wouldn’t it be great if we could just correct our mistakes in the original commit? Here’s the good news: You can!

In this article, we’ll be looking at the Fixup tool that is part of Git’s Interactive Rebase command and see how it allows us to fix mistakes in old commits.

A Few Words About Interactive Rebase

Before we go into the details and tackle an example case, I think it’s helpful to provide a very brief introduction to Interactive Rebase first.

You could say that Interactive Rebase is a set of tools that all have one purpose: They allow you to correct your commit history after the fact. You can do lots of fancy things with Interactive Rebase: delete old commits, change their messages, combine multiple commits, and much more. We won’t dive into these other use cases, but I’ll link to some good reading material at the end of this article.

One last word of warning: Manipulating your commit history, as we will do shortly, is best performed on commits that have not been shared on a remote repository yet. The reason for this is simple: If you’ve already shared commits with your teammates, chances are they have already based their new work on those exact commits. If you manipulate them after the fact, you can expect tears and swear words.

So, to live a long and happy life, I suggest you use these tools only on your local commit history — before you integrate it into a shared team branch.

#programming #software-development #git #version-control #github

How to Fix Your Mistakes in Git (and Leave No Trace)
1.05 GEEK