Conflict when merging branches in Git
When merging two branches, conflicts are possible if we changed the same file in each branch. Let's consider a situation where in both branches master and test from the previous lesson we changed and committed the same file file.txt. And then we used the merge command:
git merge test
As a result, we will see an error:
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
In addition, we will see conflict markers in the modified file:
<<<<<<< HEAD
here the changes of the master branch will be written
=======
changes to the test branch will be written here
>>>>>>> test
The top part of the message indicates the changes made in the current main branch, the bottom part - in the second branch, which we want to merge with the first. To resolve the conflict, we need to bring the contents of the file to a common form, having previously removed (by hand in the editor!) all the conflict designations that Git left in it.
After we resolve the conflict (i.e. leave the correct code in the file), we stage our file and make a commit to the master branch we are currently on:
git add file.txt
git commit -m ""merger"
Only after this we can switch to the second branch test and do all the same operations as in the branch master, including replacing the contents of the file file.txt with the same as in the first branch:
git add file.txt
git commit -m ""merger"
Now we can switch back to the main branch and merge the second one into it:
git switch master
git merge test
Intentionally create a conflict when merging branches. Then resolve it.