Software Development

Git commit fixup and autosquash

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 git rebase. Fixup commits produce commits that fix a specific commit in history by appending a commit with message fixup!. 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.

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Git commit fixup and autosquash

Opinions expressed by Java Code Geeks contributors are their own.

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button