Squashing git commits with Interactive Rebase
Posted on August 7, 2014
Squashing git commits (using git's interactive rebase mode) is a great way to trim up mistakes, typos, and overzealously granular commits. But when you first get started, it can seem very overwhelming.
I recorded a quick 5-minute video as an intro to squashing with git.
NOTE: All of the editors that pop up will use your system-wide default editor. I use Vim, but you can set it to anything you'd like.
Basically, when you're ready to squash some commits, just figure out how many commits back you'd like to include in your rebase-ing session. Let's say it's 24 commits. Now run this from your project directory:
$ git rebase -i HEAD~24
Now you're in interactive rebase mode. Change "pick" to "squash" for any lines that you want to merge into the commit above them, and then follow the prompts to set the commit messages for the new commits.
Check out the video for more details and examples.
Note: If you're actually going to be doing something like I did in the video, it'll be a lot easier to get a commit hash from git log rather than counting down 42 commits. Just copy the hash--the gibberish at the beginning of each log line--and use it like so:
git rebase -i 01j93091
Comments? I'm @stauffermatt on Twitter
Tags: git • rebase • squash