Pagine

Thursday 8 June 2017

How to solving merge conflits with git

When you are working with several branches, a conflict will probably occur while merging them. It appears if two commits from different branches modify the same content and git isn't able to merge them. If it occurs, Git will mark the conflict and you have to resolve it.
For example, Developer1 modified the myfile.txt on a feature branch and Developer2 has to edit it on another branch. When Developer2 merges the two branches, the conflict occurs.
Git will tell you to edit the file to resolve the conflict. In this file, you will find the following:
<<<<<<< HEAD
Changes from Developer2
=======
Changes from Developer1
>>>>>>> 5df2fcdc89e0a33fcf8cbbbbd89cb8eabbdb61b7
The <<<<<<< characters indicate the start of the merge conflict, the ====== characters indicate the break points used for comparison, and >>>>>>> indicate the end of the conflict.
To resolve a conflict, you have to analyze the differences between the two changes and merge them manually. Don't forget to delete the signs added by Git. After resolving it, simply commit the changes.
If your merge conflict is too complicated to resolve because you can't easily find the differences, Git provides a useful tool to help you.
The "git diff" and "git mergetool" help you to find differences. Other tools to visually compare and merge files are:

No comments:

Post a Comment