Handling Merge Conflicts with Git Rebase

When two people work on the same projects at the same time, there are bound to be situations where two people have edited the same code in two different ways. In an ideal world each member of your team would work on their own stories and discuss potential conflicts before they arise. But in the real world conflicts like this happen all the time.

The key to handling merge conflicts is to catch them early and often. It’s easier to resolve one or two merge conflicts per day than to handle 10 or 20 merge conflicts at the end of the week, after you and your teammates have forgetten the specifics of your code logic. I’m going to show you how to do mitigate issues using git rebase. 

git rebase vs git merge

First, what is a git merge? Join two or more development histories together by doing a three-way merge. You end up with a commit history that looks like this:


What is a rebase? When you rebase you take all the changes that were committed on one branch and replay them on another one.  By doing this you end up with a clean history of commits, not one with merging commits. It looks like this:


rebasing your branch from the master branch

Pretend that you’re working on a team project and you have a story assigned to you. This story is taking you 3 days to finish. During those 3 days you have team members constantly pushing their story branches and merging them with the master branch, which leaves your local branch far behind the master branch. There might even be conflicts between your edits and someone else’s edits.

One strategy to mitigate potential problems is to regularly rebase from the master branch. The important git command we’ll use in this case is $git pull –rebase origin master. This will fetch the master branch from your team repo and then rebase your branch with the master branch. If there are any merge conflicts, git will notify you so you can solve them step-by-step.

Example Process:

1) You should already have made edits and committed the edits on your own branch. In this example, my branch is named Laura-Branch-One

2) $git pull –rebase origin master


Oops, I ran into a merge conflict!

3) $git status will show us where we are


It looks like I need to check out the lib/class_three.rb file and choose which changes I would like to keep. I do this using vim.

4) $vim lib/class_three.rb

Delete everything that was added to the file and leave it with the edits you want to keep.

5) $git add.

Add your change to the rebase branch

6) $git rebase –continue


Now you’ve completed your rebase. When you’re done you can keep editing your branch or push your branch up to the team repository with a clean history.

Leave a Reply

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