Treehouse

Squash Commits with Git

By on  

I'm not a git expert but I know enough git to get by, and surely know enough git to appreciate its ease of use over svn. A while ago I published some basic git commands to that go slightly beyond basic cloning and commits, and should handle most git interactions. Today's mini git lesson involves squashing multiple commits into one, allowing for easier pull request review and merge management.

Start by making changes to the feature branch you're doing work on. Let's assume that these changes span a few commits and I want to consolidate them into one commit. The first step involves making sure the master branch is up to date with the destination repo's master branch:

# switch to master branch
git checkout master

# ensure our master is up to date
git pull remoteRepoName master

With the master branch up to date, we'll use git rebase to consolidate:

git rebase -i master

That command will show a list of each commit, as such:

pick fb554f5 This is commit 1
pick 2bd1903 This is commit 2
pick d987ebf This is commit 3

# Rebase 9cbc329..d987ebf onto 9cbc329
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Edit the summary shown to you by the rebase command, leaving the commit you want to be the main commit as "pick" and changing all subsequent "pick" commands as "squash":

pick fb554f5 This is commit 1
squash 2bd1903 This is commit 2
squash d987ebf This is commit 3

# Rebase 9cbc329..d987ebf onto 9cbc329
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Write/quit past the editor twice (the second screen would allow you to change the commit message, though I like to keep it the same). At this point, your commits are squashed into one. Run the following command to force a push of the new, consolidated commit:

git push -f

This forced push updates the source repository and our commits have become one. If you had already submitted a pull request at GitHub, the pull request would now show only one commit! With one consolidated commit, code review becomes much, much easier!

ydkjs-1.png

Recent Features

Incredible Demos

  • The Simple Intro to SVG Animation

    This article serves as a first step toward mastering SVG element animation. Included within are links to key resources for diving deeper, so bookmark this page and refer back to it throughout your journey toward SVG mastery. An SVG element is a special type of DOM element...

  • Form Element AJAX Spinner Attachment Using MooTools

    Many times you'll see a form dynamically change available values based on the value of a form field. For example, a "State" field will change based on which Country a user selects. What annoys me about these forms is that they'll often do an...

Discussion

  1. bartaz

    Please note that it’s not a very good practice to squash commits that have already been pushed to public repository (like github) – if anyone had already forked or cloned the repo before they were squashed they may end up with broken commit tree, unable to merge any new changed (without some more tricky cleaning commands)

    So it’s much better to remember to squash commits before they are pushed anywhere.

    • Is it even possible to squash after a push? I just pushed a couple of commits and thought I would play with squashing and it wouldn’t show me those commits as something I could squash.

    • It should be possible, yes. I’ve done it many times.

  2. Ashley Camba

    small note: afaik, push -f is a bad idea: http://www.randyfay.com/node/89

    unless you really know what you are doing, stay away from push force.

  3. Why all the complication? Why not just:

    git merge –squash branch-name

  4. Amit

    I think this is the most straight forward and user friendly answer. I especially like the part where you mentioned that a new screen will open and it will allow you to change your commit message. This is helpful for a user like me who isn’t familiar with git.
    Thanks for helping.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!