Git Tutorial - Merge Branches

Jinku Hu Jun 25, 2020
  1. Fast Forward git merge
  2. Recursive git merge
  3. Resolve Conflicts
Git Tutorial - Merge Branches

We will learn how to merge branches and also deal with conflicts if it exists in this tutorial.

In the last tutorial, what we did is that we created a new branch to work on some new features that we don’t want to mess up the master branch.

After we make these new branches and are happy with the new features, we need to merge it back into the master branch. So that when we publish the next release code, the new feature is also included.

Fast Forward git merge

Let’s say we are happy with the changes in new_test_branch branch and we will merge it back to the master.

  • Check out master branch
   $ git checkout master
  • Merge the branch to the master
   $ git merge new_test_branch
   Updating 5fef94e..e7a7e81
	Fast-forward
	test3.txt | 3 ++-
	1 file changed, 2 insertions(+), 1 deletion(-)

We have taken the feature from the branch new_test_branch and merged it into this master branch.

The Fast-forward shown in the message after merging means that git does fast-forwarding up to the code changes that we’ve made in the branch because in the time between branching out and merging it back into master, we have not actually made any changes in the master branch.

Recursive git merge

Before we demonstrate this different git merge, we need to create two branches test_branch_A and test_branch_B and make different changes to them. Then we will merge the test_branch_A back to the master branch.

Now we will merge test_branch_B to the master.

$ git merge test_branch_B
Merge made by the 'recursive' strategy.
 test1_rename.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

The difference between this merge and fast-forward merge is that the merge is made by the recursive strategy because this branch code doesn’t have feature test_branch_A in it. In other words, the master branch has changed after the branch test_branch_B is created.

Now master is up-to-date with both of these two features, and it doesn’t matter that we’ve worked on them at the same time, different people doing them with both just merge them back in and everything is fine.

Resolve Conflicts

What we have modified in two test branches don’t have any conflict, but what if there are conflicts when you merge some branches to the master?

Let’s create two branches test_branch_C and test_branch_D, then append text The letter should be C to the file test3.txt in branch test_branch_C and append text The letter should be D to the file test3.txt in branch test_branch_D.

Then we merge the test_branch_C back into the master branch.

Now, we will merge test_branch_D into the master,

$ git merge test_branch_D
Auto-merging test3.txt
CONFLICT (content): Merge conflict in test3.txt
Automatic merge failed; fix conflicts and then commit the result.

You could see here that it couldn’t be successfully merged because it detects the conflict in the file test3.txt. We need to manually resolve the conflict by editing the conflicting file.

The file test3.txt shows the conflict as

<<<<<<< HEAD
The letter should be C
=======
The letter should be D
>>>>>>> test_branch_D

The text between HEAD and ======= is the text in the master branch that comes from merged branch test_branch_C, which differs from the text in the to-be-merged branch test_branch_D.

You need to choose which text should keep in the master, and delete all the symbols like <<<<< and >>>>>.

Let’s say, the appended text shall be The letter should be D. We update the conflicting area to be The letter should be D only.

After resolving the conflict, we could commit the solved conflict by using git commit.

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook