Git Merge – Merging Branches Without Conflict

Commands discussed in this section:

  • git merge

When To Merge?

Examples of when would you might want to merge branches:

  • You start with a repository you cloned. After you make changes to your local copy of the repository, you will need to fetch/pull new updates from the repository and merge the updates into your local branch.
  • There are multiple branches and you want to merge the multiple branches into your branch.

Starting point

Let’s start with the repository we created in the Creating And Playing With Branches example.

On the master branch there are 3 commits:

  • Commit C1: Created the file README with the contents: This is the README file.
  • Commit C2: Added one more line to the README file: One more line.
  • Commit C3: Created the file Makefile with the contents:
    # Beginnings of a Makefile

The test branch started with the master branch commit C2:

  • Commit T1 created the file plan with the contents: My system test

The below diagram shows the 2 branches and what occurred during each commit:


Let’s reduce the above diagram to include only the commits along with the master, test and HEAD references:


Or we could use a git UI tool like gitg –all to display the commit history for the branches like this:



Merges Go To You

When you do a git merge, the result of the merged branches is placed on your current branch.

The result of the merge is a new commit on the current branch.

For example, if you want git to merge the master and test branches together and have the result placed in the master branch, then you would:

$ git checkout master
Switched to branch 'master'
$ ls
$ git merge test
Updating 423b692..eac62ab
 plan |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 plan
$ ls
plan  README

Your commit tree looks like this:


gitg –all shows the commit-tree like this:

merge without a conflict

At this point, the master and test branches have been merged onto the master branch, though you could continue to add commits to either branch. For example, from this point on, your workflow could be any of the following:

  • Continue adding commits to the master branch
  • Checkout the test branch and continue adding commits to the test branch.
  • Stop adding commits to the test branch and adds commits only to the master branch and any other branches you create.
  • etc.

Merging Without A Conflict

If the files from the different branches you’re merging don’t have any conflicts with your current branch, then the merge is done, tweet about the merge, dwell on a merge well done, and take a break.

In the above example, there were no conflicts in the merge of the two branches:

  • git merge test command was done while in the master branch.
  • The test branch had a new file named plan that didn’t exist in the master branch.
  • The file plan was copied from the test branch to the master branch
  • The merge was finished.

A merge can have no conflicts even if the files on the two branches are different, as long as the differing lines are in different locations of the file.

If you plan to merge with files that never conflict with your files, then you can stop reading about merges, and you can tweet “I’m done with the gitguys merge section!

But there are always conflicts, eventually. So put away your tweeter for now. The good news is there are a lot of tools to help the merge go smoothly.

Recent Articles

Where Do I Get Git?

Where do you get git? We have the answer to that for Linux (Ubuntu, Debian, and Red Hat), Windows, and Apple OS as well as over 30 free git tutorials.

What is the deal with the Git Index? What is the Git Index?

The git "index" is where you place files you want committed to the git repository. Learn more about the git index and git commit by selecting this link.

What Every VCS Must do

Version Control System ("VCS") need to be able to provide to its users? GitGuys explains exactly what it needs to include and provides tutorials on Git.

Visualising Branches And Their Commits

We explain and discuss the use of the git show-branch, gitg, gitk and qgit in this Tutorial. Complete with examples.

“Tracking Branches” And “Remote-Tracking Branches”

Both Tracking Branches and Remote Tracking Branches are created on the git client when a git clone command is used.

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox