banjocode Clean Your Git History With Git Fixup

Clean Your Git History With Git Fixup

The easiest way to clean up your git history by using fixup and autosquash.

2 min read

Git Fixup To Get a Clean History

We will clean up our git history with the help of two commands:

  • git commit --fixup <commit>
  • git rebase -i --autosquash

Example

It’s a pretty simple process. Imagine we have three commits like this.

We want to do some changes to Commit 2.

First of all, add your changes to the staging area

git add .

In VS Code, it looks like this.

Do your commit with the --fixup tag and the hash of the commit you want to fixup. In this case, it is the hash of Commit 2.

git commit --fixup a1633474e48c4dbafe44d85a981327e094bd7075

This will add a new commit with fixup! appended, followed by the message that the original commit had.

To finalize, we need to autosquash our work to merge the new commit to the old. We need to copy the hash of a commit before the one we fixed. In this case, Commit 1.

git rebase -i --autosquash 4be1250d7b56be16808d0a6920b60129fc7dca2e

This interactive rebase allows you to change your commits. Everything will, however, be done. The only thing you need to do is exit the editor.

Once again our git history is cleaner, and the change we did have been added to Commit 2.