How to Flatten git Commits

By  on  

One of my least favorite tasks as a software engineer is resolving merge conflicts. A simple rebase is a frequent occurrence but the rare massive conflict is inevitable when many engineers work in a single codebase. One thing that helps me deal with large rebases with many merge conflicts is flattening a branch's commits before fixing merge conflicts. Let's have a look at how to flatten those commits before resolving those conflicts!

My typical command for rebasing off of the main branch is:

# While on the feature branch...
git rebase -i master

To flatten commits before the rebase, which can make resolving merge conflicts easier, you can slightly modify the original command:

# While on the feature branch...
# git rebase -i HEAD~[NUMBER_OF_COMMITS]
git rebase -i HEAD~10

The example above would flatten the last 10 commits on the branch. With just one single commit, you avoid the stop-start nature of fixing merge conflicts with multiple commits!

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

  • By
    Jack Rugile’s Favorite CodePen Demos

    CodePen is an amazing source of inspiration for code and design. I am blown away every day by the demos users create. As you'll see below, I have an affinity toward things that move. It was difficult to narrow down my favorites, but here they are!

  • By
    Create a Quick MooTools Slideshow with Preloading Images

    I've been creating a lot of slideshow posts lately. Why, you ask? Because they help me get chicks. A quick formula for you: The following code snippet will show you how to create a simple slideshow with MooTools; the script will also...

Discussion

  1. Jeff Berman

    Some of the code in my previous comment got stripped out. The command I use to merge a branch into master while flattening it to a single commit is:

    # While on the master branch...
    git merge --squash branch_name
    

    Seems there are always multiple ways to do things, especially in Git.

  2. Also it’s possible to select all commits that belongs to this branch after creation from the parent branch and don’t select count of them by hands with the following:

    git rebase -i git merge-base --fork-point master
    

    It’s useful if you want to do that prior to merge

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