Merging lets developers bring their changes onto an individual line of development. To merge two branches, you can use the git merge command. Once you run this command, the changes from one branch are merged into the branch you are viewing.
This tutorial will explore—with examples—the basics of merging. We’ll discuss why you may want to merge your code, and how to use the git merge command to merge your code.
Git Merge: An Introduction
The git merge command lets you combine two different branches of development. Developers usually combine branches once they have made all the changes you want to make to a branch.
For instance, say you may have one branch that stores the main code for a project. Another branch stores the code for a bug fix. 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. Pushing your code means that everyone will be able to access your changes. 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 Git commit called a merge commit. This merge commit will combine the changes made to both branches and save them to the main branch.
Merge commits, like any commit, require a commit message. When you initiate a merge, Git will ask you to specify a message. A default message will be populated that describes the two branches you are merging. You can change this default message.
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:
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:
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.
Git Merge Branch to Master: 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. This will let you make sure that you are viewing the branch on which you want to perform a merge. If you want to merge a branch to the master branch, you’ll want to make sure that you are on the master branch.
Let’s run the git status command to confirm we are viewing the right branch:
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>
The 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.
Next, 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. This is necessary for a successful merge to occur.
git fetch git pull
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>
The 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. 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:
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.
Git Merge Conflicts
A merge conflict is where you have changed the same part of a file on both the branches you are trying to merge. 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. Git will ask you to resolve the conflicts that have appeared manually. This is because Git does not know what changes you want to save and which ones you want to discard. 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:
On branch master Unmerged paths: (use "git add/rm ..." as appropriate to mark resolution) both modified: index.html
We know that there is a conflict in the index.html file. This merge conflict occurred because we have modified the file on both the 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. Any 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. This command will add the conflicted files to the staging area. Then, commit those changes using the git commit command. Once you have committed your changes, they will show up in the history of your repository.
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.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more