A branch is a pointer or reference to a commit. Branches help developers create an independent workspace and develop and test new features. When the development on a branch is completed, it is merged with the master branch.
Merging is the process of combining two branches into a single one. It takes the changes from the other branch and adds them to our current branch. The git merge command helps us to merge branches.
Sometimes the merge process fails and results in a merge conflict. Merge conflicts predominantly occur when we pull changes from the remote repository. Let's learn what causes merge conflicts and how to resolve them.
A Merge Conflict occurs when two or more developers have altered the same line of code in the same file. When we try to merge these different versions of the same file, Git cannot decide on which version of the file to keep and which to discard. Git leaves it up to the developers to decide which version to keep. In these cases, the merge process starts but will not complete.
A conflict may also arise when we have pending changes in the working directory or staging area(index) of the branch that we are trying to merge. In such cases, the merge will not start.
When a conflict occurs, Git returns the following error message.
Auto-merging <file-name>
CONFLICT (content): Merge conflict in <file-name>
Automatic merge failed; fix conflicts and then commit the result.
The best way to resolve merge conflicts is to try to avoid them in the first place. Following are some of the best practices to be followed.
Push and pull changes as often as possible.
Create new branches instead of directly adding commits to the master branch.
Keep your changes small.
Commit often.
However, we can't always prevent a conflict. Thankfully, Git helps us to resolve conflicts. Git will tell us the file name and highlight the lines that led to the conflict. Git alters the conflicting file and adds some special characters(<<<<<<<, =======, and >>>>>>>) to indicate the lines that lead to the conflict.
The best way to resolve merge conflicts is to talk to other developers about the changes. Git tells us about the conflicting code lines, and we can discuss these changes with other collaborators and decide on which change to keep and which one to discard. After deciding on which version of the file to keep, make the changes in the file and add and commit the changes. It will resolve the conflict.
Or we can abort the merge by running the Git Merge --abort command.
git merge --abort
Let's create a merge conflict in our local repository.
Consider a master branch and a file called demo.txt. Let's view the contents of the file by using the cat command.
cat demo.txt
demo.txt:
Line 1
Line 2
Line 3
Now, create and check out a new branch called feature.
git checkout -b feature
Let's modify the demo.txt file from the feature branch and add a commit. The modified file is shown below. Note that these changes exist on the feature branch and not on the master branch.
demo.txt:
Line 1
Line 2 from feature branch
Line 3
Next, check out the master branch and alter the same line(Line 2) in demo.txt. Create a new commit for these changes. The modified file on the master branch is shown below.
demo.txt
Line 1
Line 2 from master branch
Line 3
Let's merge the feature and the master branch. A merge conflict will occur because the master and the feature branch contain changes in the same line(line 2) of the same file(demo.txt).
git merge feature
Output:
Auto-merging demo.txt
CONFLICT (content): Merge conflict in demo.txt
Automatic merge failed; fix conflicts and then commit the result.
The file is altered by Git. The text between <<<<<<<HEAD and ======= is from the master branch. The text from ======= to >>>>>>>feature is from the feature branch.
demo.txt:
Line 1
<<<<<<< HEAD
Line 2 from master branch
=======
Line 2 from feature branch
>>>>>>> feature
Line 3
We discuss the conflict with our fellow developers and decide that we want to keep the following version of the file.
demo.txt
Line 1
Line 2 from master branch
Line 3
We modify the file to match the above version and add and commit the change.
git add demo.txt
git commit -m "Resolve Merge Conflict in demo.txt"
We will perform the Git Merge operation several times during the development of our project. Merge conflicts will also occur multiple times. Git provides details like the file name and lines that caused the merge conflict. The best method to resolve merge conflicts is to discuss them with your team. Decide on which version of the file is to be kept and discard the other version. Then, make the changes in the file and commit the changes.
There are tools available that can help us visualize and resolve merge conflicts. Some of the popular tools are mentioned below.
SourceGear DiffMerge
Perforce Visual Client
Meld
P4Merge