tl;dr

No one wants to see all the churn that got you to your final destination. Squash your commits.

Squash

I wasn’t exactly sure what I wanted to do before starting to write the code for a feature. I know, I know. Eventually, I got to a state I liked, but there were some missteps and bad commit messages along the way.

0ac78424ea746f1e70f0597f96b781ca444577ca issue-123: combine docs into one file
13a81579d74442d6364a9deb2f1a1625680ac0df add barrrrrrr
078670244fc4d23f39f66b2c36354e263aee2c23 moved foo to docs
61ce6122cef24038f8d5ec500db45f0e726423fd add content to foo
4250e3d11f092f52dedb625011d7db18aa06243a add foo placeholder

Is there any value in sharing this garbage with the rest of the team?

No.

This is just noise. When my feature hits the main branch, it should be accompanied by a concise, clean commit history. So how do I clean it up?

Squash It

I can squash the commits, which means I can combine them and modify the commit messages. As ever, there are like 600 variations to this git command, but this example

git rebase -i upstream-branch

starts an interactive rebase on the commits in my branch that have not gone into upstream-branch yet.

First, I see a screen asking want to do with the commits. I change the commands to pick the first commit and squash the remaining into it.

pick 4250e3d add foo placeholder
squash 61ce612 add content to foo
squash 0786702 moved foo to docs
squash 13a8157 add barrrrrrr
squash 0ac7842 issue-123: combine docs into one file

Next, I see a screen that has all the old commit messages. This is useful in crafting a new message. My messages are all pretty much crap and don’t help describe the final state, so I just comment them out. Whatever is left will be the new commit message for the picked commit above.

# This is a combination of 5 commits.
# The first commit’s message is:
# add foo placeholder

# This is the 2nd commit message:

# add content to foo

# This is the 3rd commit message:

# moved foo to docs

# This is the 4th commit message:

# add barrrrrrr

# This is the 5th commit message:

issue-123: add docs

Now all the changes have been squashed into a single simple commit.

commit 6bcf71ff972606d625c25c860a67ec5526d124fb
Author: Jeremy Greer <jeremy.greer@theinterwebs.com>
Date:   Thu Jan 11 08:14:41 2018 -0600

    issue-123: add docs

Squash

But I already pushed!

If I’d had any significant amount of work, I would have already pushed my branch to origin. I can’t very well rewrite the history on a remote branch, can I?

Well, no. I probably shouldn’t. If anyone’s using my “private” branch, they’re going to be upset when I change the history with

git push --force

We’ll save that discussion for another post.