Git — Automatic Squash

Mariano Matayoshi
Aug 25, 2017 · 2 min read

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.

You can follow me on Twitter, StackOverflow, LinkedIn or Github :)

Cheers!

)

Mariano Matayoshi

Written by

Love to share my knowledge with friends and co-workers. Self-Taught. Devops enthusiast. Shortcuts and startup culture lover. Always learning and improving.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade