Git rebase – How to clean merge history log and squash commits

Cleaning up merge mess

I have people working on different branches. Each person has made several commits.

They’ve then merged their work into master branch. Leaving the master branch with several merge messages along with commits and a confusing graph.

When you look at the commit history after merging, it looks like below.

Change 1, change 2 were on new_branch_2. Now imagine 100s of commits like these. 

How would this look if the person rebased their branch before merging:

As you’ll note here, the two commits from new_branch_2 were simply added on top of the master branch last commit. 

This is a very simple example but as soon as you start getting more branches and commits, git log quickly becomes a mess to look at.

To begin interactive git rebase, get on new_branch_2 and run: git rebase -i master 

Turning multiple commits into a single commit:

Sometimes you’ve made multiple commits, fixed, Reverted, finally done. Haha, classic! 

Before you merge this into the master branch, you could turn this into ONE commit called Fixed.

So you’re not polluting the master branch with commit messages that shouldn’t really be public!

Before squashing the commits:

After squashing the commits:

Squashed change 1, change 2, final change, finally fixed and // tested and fixed the bug INTO: The bug is now resolved.

Looks better!

I have now merged all 5 commits into a new commit. Clean!

To begin squash run:

git rebase -i HEAD~5

This will take the last 5 commits, you replace “pick” with “s” or “squash” for 4 of them, all 4 of them will be merged into one available commit.

For example:

pick f8313c5 change1

s 471ef8c change2

s cae20d7 final change

s ea021c3 finally fixed

s ea021e3 // tested and fixed the bug

Once you’ve edited the rebase interactive file, save it.

New window should open. In this window, delete commit messages leaving a single message. This will make sure your final commit has one message only.

Finally rebase again and merge!

Protip: Don’t mess with other peoples branches unless you have to!

Download a free copy

Leave a comment

Your email address will not be published. Required fields are marked *