Git - Automatic Squash

Published Aug 14, 2017Last updated Aug 17, 2017

Hello!
Everyone has, at one point or another, committed something like: X thing fixed, Little fixes, or More fixes while developing a feature. These messages don't have much meaning in and of themselves, but were most likely part of a branch named new-feature, with a history that looks something like this:

16f612a Last fix
14f9f9a More fixes
16f989a Feature A

If Feature A were finished, we'd merge the new-feature branch to master. The last two messages are now in the history of master, but by themselves, they lack meaning.

To avoid this, I started to use the option --fixup when committing, like this:

git commit -— fixup <commit_hash>

This command automatically marks the commit as a fix of the commit with hash commit_hash, so the new commit will have the message: fixup! Feature A. Instead of having the messages More fixes and Last fix, we're going to have this:

16f612a fixup! Feature A
14f9f9a fixup! Feature A
16f989a Feature A

Yes, I know what you're thinking, but we're not going to merge these commits to master.

Before merging to master, we're going to rebase this branch with the --autosquash option

git rebase -i --autosquash HEAD~2

You'll see something like this in your editor of choice:

pick 16f989a Feature A
fixup 14f9f9a fixup! Feature A
fixup 16f612a fixup! Feature A

This command will squash our three fixups commits into one — in this case — into a new commit with the message Feature A.
After the running the rebase, our history will be:

16f989a Feature A

Now our branch is ready to be merged into master without irrelevant messages.

I hope that this command makes your workflow easier.

Cheers!

Discover and read more posts from Mariano Matayoshi
get started
Enjoy this post?

Leave a like and comment for Mariano