Posts

  • Squashing top commits

    Sometimes you find yourself wanting to squash the top commits of a branch down to just one commit. Most of the time this can be accomplished with an interactive rebase:

    $ git log -n 3 --oneline
    474142a Update link to Pro Git in CONTRIBUTING page
    3a758a8 Update link to Pro Git in CONTRIBUTING page
    b22ae59 update org name in links
    
    $ git rebase -i b22ae59
    

    Changing the keyword pick on the second line to fixup, or f for short, will squash the two commits into one.

    pick 3a758a8 Update link to Pro Git in CONTRIBUTING page
    fixup 474142a Update link to Pro Git in CONTRIBUTING page
    

    This should result in something like:

    $ git log -n 2 --oneline
    4c3d80a Update link to Pro Git in CONTRIBUTING page
    b22ae59 update org name in links
    

    Sometimes rebasing directly is not feasible, e.g. when you have a botched merge commit that you have fixed, but later realized you wanted to roll into the same commit. In this scenario you can usually use a combination of reset and commit --amend. Using the same example as above:

    $ git log -n 3 --oneline
    474142a Update link to Pro Git in CONTRIBUTING page
    3a758a8 Update link to Pro Git in CONTRIBUTING page
    b22ae59 update org name in links
    
    $ git reset --soft 3a758a8
    $ git commit --amend -C HEAD
    

    reset --soft will rewind the current branch, but leave the index intact so that commit --amend can update the first commit. Adding -C HEAD will reuse its commit message. The end result should be similar:

    $ puter:spring-framework(master) ms$ git log -n 2 --oneline
    46584d3 Update link to Pro Git in CONTRIBUTING page
    b22ae59 update org name in links
    

subscribe via RSS