Merge Conflicts
Learn about merge conflicts and how they occur by reading through this lesson.
We'll cover the following
What is a merge conflict?
Merge conflicts occur, most commonly, when more than one contributor is working on a project. A merge conflict takes place when a file changes at the same line in different branches or if a file is deleted in one branch, but in another, its contents are updated. When the branches are merged, Git won’t be able to infer which change it should keep and which one it should discard. At this point, it becomes necessary for a developer to resolve this merge conflict.
How a merge conflict can occur
Let’s look at an example of how a merge conflict can occur. We will create a new branch called feature_branch
and switch over to it from master
using the following command:
git checkout -b feature_branch
Now that we’re at the feature_branch
, we will update file1.txt
.
echo 'Updated file1 in feature_branch' > file1.txt
Next, we will create a new commit and make sure that the change in file1.txt
becomes part of the snapshot by entering this command:
git commit -m 'updated contents of file 1 in feature_branch'
We will then switch back over to master
and update file1.txt
again:
echo 'Updated file1 in master' > file1.txt
We will proceed to create another commit that will contain the new change but this time for the master
branch.
git commit -m 'updated contents of file 1 in master'
Try entering the commands mentioned above in the terminal provided below. You are already provided with a working project directory and initial commit:
Get hands-on with 1200+ tech skills courses.