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
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.
Let’s say we are happy with the changes in
new_test_branch branch and we will merge it back to the
$ git checkout master
Merge the branch to the
$ 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
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
Before we demonstrate this different
git merge, we need to create two branches
test_branch_B and make different changes to them. Then we will merge the
test_branch_A back to the
Now we will merge
test_branch_B to the
$ 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.
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.
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
Let’s create two branches
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
Then we merge the
test_branch_C back into the
Now, we will merge
test_branch_D into the
$ 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.
test3.txt shows the conflict as
<<<<<<< HEAD The letter should be C ======= The letter should be D >>>>>>> test_branch_D
The text between
======= 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
You need to choose which text should keep in the
master, and delete all the symbols like
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