Git commit fixup and autosquashsunday, april 22, 2018
Git commit fixup and autosquash are helpful features when you want to “fix” changes from a single commit in your history.
If you discover that you want to change a single commit in your Git history, you would need to manually commit based on the commit you’re about to change and perform a
Fixup commits produce commits that fix a specific commit in history by appending a commit with message
An interactive rebase with
--autosquash option will then merge the original commit and the fixup into a new commit and rebase the subsequent commits.
See the following history as an example:
3320dec (HEAD) commit 4 03c9685 commit 3 041c401 commit 2 981fffd commit 1 22f759b (tag: base) initial commit
We can modify changes introduced in
981fffd commit 1 and add them as a fixup commit via
git commit -a --fixup 981fffd:
c24491b (HEAD) fixup! commit 1 3320dec commit 4 03c9685 commit 3 041c401 commit 2 981fffd commit 1 22f759b (tag: base) initial commit
In order to clean-up the history we interactively rebase our changes with
git rebase --autosquash --interactive base.
This will produce a clean history again:
caeb1d8 (HEAD) commit 4 2f6d4da commit 3 8207cf2 commit 2 551ef47 commit 1 22f759b (tag: base) initial commit
The commit hashes after
22f759b now have been changed — they’re based on different commits.
To avoid breaking the history of colleagues you should only change the branch’s history, e.g. via rebase, if the commits haven’t been pushed yet, or if you’re working on a dedicated remote branch.
This post was reposted from my newsletter issue 023.
You’re interested in more productivity and command line tips? Then you might enjoy my Developer Productivity Masterclass.