Welcome to Abdul Malik Ikhsan's Blog

Practical GIT (4) : Rebasing Conflicted “task” branch against primary branch

Posted in GIT by samsonasik on September 16, 2015

git-logoWhen we work on our “task” branch that based on our primary branch, We may have overlapped commits when our team have update the primary branch.
Many people just use “merge” the primary branch into their “task” branch, but it will make extra commit that contains actually same changes, that may a project history not clean, and make hard to review (for example: in case we use merge or pull request). What we need to do is rebase instead. Actually, the merge should only happen when you need to apply your “task” branch into your primary branch.

Rebase will synchronize the commits and put our changes in top of commits. Let’s see what the process step by step. For example, you work with git flow, you’re now on “feature/xzy” against develop.

$ git flow feature start xzy
# can be replaced with 
# git checkout -b feature/xzy develop

And we are working on a big task. On the middle of the day, someone or some people on the team make a hotfix that make a commit to master and develop, that make your branch is outdate, and has some conflict.
What you can do to synchronize is by using rebase with steps:

1. Checkout primary branch (at this case: develop), pull latest

$ git checkout develop
$ git pull origin develop

2. Back to our feature branch and rebase

$ git checkout feature/xzy
$ git rebase develop

From now on, if we see conflict, and get error:

You are currently rebasing.
  (fix conflicts and then run "git rebase --continue")

We need to see the files that contains conflicts that may contains:

<<<<<<< HEAD

, and fix them. After that, we can close the editor. Don’t make a commit after fixing conflicts in the files!

3. Add resolved files and you can continue

$ git add .
$ git rebase --continue

4. Last, if we publish the “feature/xzy” branch into our origin/feature/xyz, we need to push –force because the commits has been rewritten:

$ git push --force origin feature/xzy

If everything ok, then we are now synchronized. That’s it😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: