Working across multiple branches is common in Git repositories. Once you’ve finished working on changes in one branch—such as adding in a new feature—you’ll likely want to save the changes you have made to the main copy of your code.
That’s where merging comes in. Merging is a feature of Git that allows you to combine the history of two branches together, and create one single branch.
This tutorial will explore—with examples—the basics of merging, when you may want to merge your code, and how to use the git merge command to merge your code. By the end of reading this tutorial, you’ll be an expert at using the git merge command.
Merging Code in Git
Merging code is an important feature of software development with Git that allows you to combine two different branches of development after you have made all the changes you want to make to a branch.
For instance, you may have one branch that stores the main code for a project, another branch that stores the code for a bug fix, and yet another branch that stores the code for a new feature on which you’re working.
After you’ve finished the bug fix, you may want to push that code to the main branch, so everyone is working with the most up-to-date code. To do so, you would need to merge the code from the bug fix branch into the main branch for your project.
The git merge command is used to merge two target branches into one in the Git version control system. The command accepts two branches and will create a special commit called a
merge commit. This merge commit will combine the changes made to both branches and save them to the main branch.
Code Merging: Step by Step
Let’s walk through a step-by-step example of merging code in Git.
Suppose we have a branch called
fix-issue22 which stores a version of our codebase where we have been fixing a bug. This branch was based on the
master branch, which stores the main code for our repository.
We can visualize this scenario through the following graphic:
In this graphic, you can see we have two branches of development. The
fix-issue22 branch is two commits ahead of the
master branch because we have been fixing an issue on that branch. Now, we want to merge these two branches together.
When we merge our two branches together, a merge commit will be created. This will result in the following change to our codebase:
In the above image, you can see that a new commit has been added to our master branch. This commit is based on the changes made in our
fix-issue22 branch, as well as the code in our master branch.
When we executed our merge, our two branches were combined together.
Git Merge Example
Now that we know how merges work, we can explore an example of the git merge operation. Let’s break down the git merge operation into its main steps.
Step #1: Preparing a Merge
Before you merge code in a Git repository, you should prepare the repository.
The first thing you should do is run git status to make sure that HEAD is equal to the branch that will be receiving the merge. So, if you want to merge a branch to the
master branch, you’ll want to make sure that you are on the master branch.
In our case, when we run the git status command, the following is returned:
On branch master
This tells us that we are already on the master branch. If we were not, and we wanted our master branch to receive our merge, we would need to run:
git checkout <branch name>
branch name refers to the name of the branch to which you want to switch. You can learn more about the git checkout command and how it works in our git checkout guide.
After this, you need to make sure you have fetched all the latest commits from a remote repository. This step makes sure your local copy of a codebase is equal to the remote repository, which is necessary for a successful merge to occur.
Step 2: Executing the Merge
We have just prepared our repository for a merge, and so we are ready to start executing the merge command line operation. To do so, we should use the following command:
git merge <branch name>
branch name parameter refers to the name of the branch you want to merge into the receiving branch. The receiving branch is the branch that you are currently viewing (which we set to be
master in the last step).
Git Merge Types
There are two types of merge which can occur when the git merge command is executed: a fast-forward merge and a three-way merge.
A fast-forward merge is a merge that occurs when there is a linear path between the two branches you want to merge.
If the receiving branch has not diverged from the branch you are merging, a fast-forward merge will occur. This means that instead of creating a new commit, the Git repository will point the receiving branch to the latest commit of the branch you are merging.
Let’s use our example from earlier to illustrate how this works. Suppose you have a repository with two branches: master and fix-issue22 (which is based on the master branch). If the fix-issue22 branch has been changed, but the master branch has remained the same, a fast-forward merge can occur.
Here is a graphic representation of our fast-forward merge:
A fast-forward merge can occur because the fix-issue22 branch is based on the master branch, which has not changed. So, all Git needs to do is create a pointer on the master branch which reflects the changes made on the fix-issue22 branch.
However, it is not always the case that a fast-forward merge can occur. If the master branch in our example has changed, as well as the fix-issue22 branch, then a three-way merge (3-way merge) needs to occur.
A three-way merge uses a new commit to merge the changes from two branches together.
Suppose we have a repository with two branches: master and fix-issue22. If we have pushed commits to both the master and the fix-issue22 branches, then a three-way merge will need to occur. This will create a new commit that merges the changes between these two branches.
Here is a visual representation of a three-way merge:
In this graphic, you can see that there have been commits on both the master and the fix-issue22 branches. Then, when we go to merge our branches, a new commit is created which contains the changes made to both branches.
When you’re merging two branches together, you may encounter a merge conflict.
This refers to a situation where the two branches you are trying to merge have both made changes to the same part of the same file. In this scenario, Git will not be able to figure out which version to use. Merge conflicts can only occur on three-way merges.
When a conflict occurs, the merge operation will stop and Git will ask you to resolve the conflicts that have appeared manually. This gives you an opportunity to choose which version of a file should appear in the final merge commit created by Git.
To find out about the conflicts that have been identified, you can use the git status command after you have started a merge.
For instance, suppose that we made a change to a file called index.js on both the master and fix-issue22 branches of our hypothetical repository. We could run this command to see the merge conflicts that have appeared:
This command returns, in our case:
On branch master Unmerged paths: (use "git add/rm ..." as appropriate to mark resolution) both modified: index.html
Now we know that there is a conflict in the index.html file which has occurred because we have modified the file on both branches we are trying to merge.
In Git, conflicts are presented directly on the affected files. This occurs through visual markers which are added around the conflicts in the Git repo.
The conflict markers you can expect to see are as follows:
The code before the = marker is the code on the receiving branch, and the code after the = marker is the code on the merging branch. The > and < markers are followed by the branch names and denote which code appeared on what branch. When you see these markers on a repository, you can update the file to reflect the changes you want to make.
Once you have fixed the conflicts identified by the git merge command, you can run the git add command to add the conflicted files to the staging area, then commit those changes using the git commit command.
The git merge command is used to execute a merge operation on a Git repository. Merging is the process of combining two branches and creating one branch which reflects the changes made across those branches. There are two types of merge that can occur: fast-forward merges and three-way merges.
In this tutorial, we’ve discussed the basics of merging code in Git and how to use the git merge command. Now you’re ready to start using the git merge command to merge your code like a professional.
It can be difficult to know where to start in your journey to a career in tech. Download the free Career Karma app today to talk with one of our expert career coaches about potential starting points for your new career transition.