When merging branches, you need to guide Git on how to resolve conflicting changes in different branches.
A conflict occurs when Git cannot automatically reconcile different changes made to the same part of a file.
A merge conflict is a type of conflict that happens when Git can't automatically combine changes from two branches because the same parts of a file were modified differently in each branch. When this happens, Git pauses the merge and marks the conflicting sections in the affected files so you can resolve them yourself. Once you've reviewed and resolved the conflicts, you can tell Git to proceed with the merge.
Preparation We need a repo with two branches containing conflicting changes. Given below is how you can create such a scenario:
- Create a repo named
nounswith one commit. - Start a branch named
fix1in the repo. Create a commit that adds a line with some text to one of the files. - Switch back to
masterbranch. Create a commit with a conflicting change i.e., it adds a line with some different text in the exact location the previous line was added.
The above can be done with the following commands:
md nouns
cd nouns
git init
echo -e "blue" > colours.txt
git stage colours.txt
git commit -m "Add colours.txt"
git switch -c fix1
echo "green\nred\nwhite" >> colours.txt
git commit -am "Add green, red, white"
git switch master
echo "black\nred\nwhite" >> colours.txt
git commit -am "Add black, red, white"
The result will be something like this:
gitGraph BT:
%%{init: { 'theme': 'default', 'gitGraph': {'mainBranchName': 'master'}} }%%
commit id: "Add colours.txt"
branch fix1
checkout fix1
commit id: "[fix1] Add green, red, white"
checkout master
commit id: "[HEAD → master] Add black, red, white"
As you can see from the above, both master and fix1 branches are modifying the same file at the same location. The master branch is inserting black in the same place the fix1 is inserting green.
blue
black
red
white
master branch]
blue
green
red
white
fix1 branch]
Target Merge the two branches while reconciling the conflicting changes in the two branches.
1 Try to merge the fix1 branch onto the master branch. Git will pause mid-way during the merge and report a merge conflict. If you open the conflicted file colours.txt, you will see something like this:
blue
<<<<<< HEAD
black
=======
green
>>>>>> fix1
red
white
2 Observe how the conflicted part is marked between a line starting with <<<<<< and a line starting with >>>>>>, separated by another line starting with =======.
Highlighted in yellow in the box below is the conflicting part that is coming from the master branch (note the HEAD label in line 2, which indicates this conflicting change is in the currently active branch, which is the master branch):
blue
<<<<<< HEAD
black
=======
green
>>>>>> fix1
red
Similarly, this is the conflicting part that is coming from the fix1 branch:
blue
<<<<<< HEAD
black
=======
green
>>>>>> fix1
red
3 Resolve the conflict by editing the file. Let us assume you want to keep both lines in the merged version. You can modify the file to be like this (i.e., remove the lines with conflict markers while keeping both lines black and green):
blue
black
green
red
white
Steps for resolving a conflict, in general:
- Remove conflict markers (e.g.,
<<<<<< HEAD) - Edit the remaining text any way you see fit (e.g., keep/edit/delete one or both; you can even insert new text).
If there are multiple conflicts (in multiple files, or in different locations within the same file), resolve them in a similar fashion.
4 Stage the changes.
5 Complete the merge by doing one of the following:
- Option 1: Commit the staged changes (as you would do normally).
- Option 2: Ask Git to resume the merge using the command
git merge --continue.
done!