Merge Conflicts
Last updated
Last updated
Merge conflicts occurs when two (or more) modifications (made by yourself or others) are made to the same line of a file. This causes a state of confusion as Git is unsure which change should be applied. So rather than making a decision for you, it "errors" and lets you decide.
To make this more concrete, let's fabricate a merge conflict.
Firstly, create two new branches from main
:
Then, on branch-A
, edit the line Hello World
by changing it (e.g. Hello
). Remember to commit the changes!
Then, on branch-B
, perform another edit on the same line but to a different text (e.g. Hello Universe
)
Finally, switch back to main
and merge both branch-A
and branch-B
together:
Merging branch-A
should not have any issues but merging branch-B
will have the following error:
This means that Git is not entirely sure if it should keep the changes from branch-A
or the changes from branch-B
so it lets you decide.
You can use git status
to view the state of the repository now that it has a merge conflict:
Notice that it indicates that hello.txt
is unmerged. Only files with merge conflicts are left in this area. Any other files without a merge conflict will be merged as per usual.
To resolve the merge conflict, open up hello.txt
in your favorite text editor.
You will notice the following (or something similar depending on your changes):
Git displays both changes (delineated by the ======
) and all you need to do is to edit the area to remove the <<<<<<< HEAD
, =======
, and >>>>>>> branch-B
. You may choose to delete the content in the top half, bottom half, both halves, or keep both.
Notice that the top half is the current content in the branch and the bottom half is the content that is about to be merged.
For the sake of simplicity, let's keep both changing by only removing the new lines:
Then, save the file and close the file. Then, add the file to the staging area:
Now, you can perform git status
once more to view the status:
The issue has been resolved and so Git no longer has a dedicated section for the unmerged files.
Finally, complete the merge by using git commit
. You may notice that the commit message is already provided for you so you can just accept it as it is.
And there you have it, we have successfully caused and resolved a merge conflict! Now, onto the collaborative workflows that Git allows.