Skip to content

Instantly share code, notes, and snippets.

@mikedfunk
Last active February 22, 2018 13:31
Show Gist options
  • Save mikedfunk/9245492 to your computer and use it in GitHub Desktop.
Save mikedfunk/9245492 to your computer and use it in GitHub Desktop.
Squashing working commits

Squashing working commits

Let's say you're working on the whizbang feature. You branch off of develop to the new feature/whizbang branch and start coding. You commit with various things along the way. You commit one time and realize you broke it, so you fix it and commit again. Several commits later it's all set, tested, and ready to merge into develop. How do you squish all those intermediate commits into one, finished commit?

From your feature/whizbang branch, type git rebase -i develop

Then you'll get a rebase editor with some helpful comments as a cheatsheet:

Imgur

The top one is the first commit after branching off of develop. We're going to modify the commit message to have the issue number and all that. change the word pick to reword or just r.

The rest of them are commits you made after you branched off of develop. change the word pick in front of each of them to fixup or just f. This means we are discarding their commit messages. It should now look like this:

Imgur

Save and quit with :wq. It will bring up another editor and prompt you to change the commit message for the r commit:

Imgur

Change the first line to something like my reworded commit message, save, and quit with :wq. Now checkout develop in git and merge in feature/whizbang. Then check the git log. You'll see that only the reworded commit is there and the extraneous ones are gone. Sweet!


Alternate method

Another way to do this is to check out develop, do git merge --squash feature-whizbang, and commit with your final message.

Downsides of this method

  • This doesn't fit into a pull request workflow because you're committing directly on develop.
  • You'll have to use git branch -D to delete the feature-whizbang branch because git thinks it's not fully merged.
  • It squashes the commits in develop, but not in the original branch, which you may not want.
  • Git doesn't know you merged in a feature branch this way, it just thinks you committed directly on develop. You might not like how this looks in your git graph - It looks like a branch was made and committed to but never merged back in.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment