Skip to content

Git: Squashing commits

Anthony edited this page Oct 10, 2016 · 4 revisions

Problem

I made a bunch of commits for a single feature. I would like to squash them into a single commit before I make a pull request, so as to make the intent of the pull request clearer.

Commands

If you are using Cygwin, please fix VIM with the commands in the "Fix VIM in Cygwin" page of this wiki.

You're going to revise some history in the master branch. First, type git log to find the earliest commit you would like to consider. Go one previous to take as the base. Copy the long commit hashcode into your clipboard.

git rebase -i <hashcode>

This will open a VIM environment with the list of commits following the base commit you chose. The goal here to list the "squash" command before the commits that you would like to meld into the previous one.

  1. Press "i" to start inserting text
  2. The commits are listed earliest to latest. Find the ones you would like to meld. Change "pick" to "squash" before those commits.
  3. Press escape to exit INSERT mode
  4. ":wq" and ENTER to save this file

The rebase will now do its work. For every squashed bunch, it will present you a commit message to edit. Here are my suggestions for editing it:

  1. Press "i" to start inserting text
  2. Above the top comment, make a batch comment. What's the concept you're grouping here?
  3. Place "* " before each of the grouped commit comments, so they appear as bullets under the main concept.
  4. Delete commit comments that aren't helpful, like "fix" or whatever
  5. ESCAPE, and then ":wq" then ENTER to save and get out of there

Resources

https://www.atlassian.com/git/tutorials/rewriting-history

Clone this wiki locally