Learning How to Git: Merging Branches and Resolving Conflict

In some previous stories, we have talked about branches. So, let’s say we already working with a lot of developers and have a lot of branches. If we only use those branches just the way they are we are going to see multiple branches with different states. So, what if we want to combine those files and differences? We can use the merge command.


The above visualization describes merging branches in Git. In the left figure, there are two branches named: master and feature. Then let’s say we want to merge the commits from feature to master. After the merging operation, we are going to have the commits from feature available in the master branch. To understand it better, let’s try it by our self.

Checking our Branches

  • Note: I have add a new commit in the master branch

To check the log of our commits from all branches, we can use a bit more complicated command. The command we are going to use git log --oneline --all --graph --decorate. I don’t remember each parameter function, but that’s the prettiest git log command and only showing only the important information. Let’s try to run it.

We can see that there are two ‘branches’ in above picture. The first one is the master branch (the branch on the left), the second one is the dev branch (the branch on the right). We can see that there are two commits that are not synced yet in those branches. Since the one that being released to the public is master, so let’s merge the commit from dev back to master.

The Actual Merging

Finally, we are going to actually merge it. Keep in mind that if we want to merge two branches, we have to be in the destination branch. So if we want to merge dev to master, we need to be in the master branch. If you want to know on which branch are you, you can use git status. Then the only thing run is the git merge <name-of-branch>. So, because we want to merge the dev branch, we are going to execute git merge dev.

Bonus: Resolving Conflict

Uh oh…

Sometimes, when we try to merge two branches we are going to face a git conflict. Conflict is the state where certain line(s) in a certain file have different changes in the commits in different branch and git can’t decide which one is the right changes (also if both changes are correct and used). So, we are going to check the file that has the conflicted copy which in this case README.md.

The conflicted file

Most probably your conflicted file(s) will have marks such as the above picture. The marks divide the file state between the destination branch and the branch that being merged to. In this case, the HEAD one will be the destination branch which is the master branch, where the other part will be the dev branch as indicated at the end of the mark.

To resolve the conflict, all we need to do is to edit as we need it. So, the one that resolving the conflict should know which line of code to keep and which line of code to discard. In my example, we will take all of the code (nothing discarded).

After we’ve done with resolving the conflict, run a git commit once and we can commit the merge process as what we called the merge commit.

Creating a merge commit

We’re done! If then we check the log of the git, we will see that it is merged once again,

Those two branches already merged

Wrap Up

Today, we have talked about merging branches in Git. Actually, from all of previous stories up until this story you already can collaborate and work with Git with your team or partners. For future stories there will be more advanced git usages. Hopefully, it may help you start working with Git and thanks for reading.