Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix conflicts in git?

When I try to pull I have the following message:

Automatic merge failed; fix conflicts and then commit the result.

So, I try to fix conflict (as git suggest) for that I follow these instructions.

Among the first things that I need to do is to open the "problematic" file in a text editor and find lines like that

If you have questions, please
<<<<<<< HEAD
open an issue
=======
ask your question in IRC.
>>>>>>> branch-a

The problem is that I do not see things like that in my file. In particular I searched for HEAD and it is not there.

like image 594
Roman Avatar asked Dec 01 '17 07:12

Roman


1 Answers

There are several ways to solve git conflicts, I will explain here using GUI and text editor way.

Solving git conflict using text editor.

Open any one of your favourite editor, example, Notepad, gedit, vim, nano or even Eclipse.

  1. Whenever there is conflict in a file git add conflict marker that looks like this <<<<<<<<.

  2. When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD

  3. ========, it divides your changes from the other branch as >>>>>>>>YOUR_BRANCH_NAME

  4. You can decide if you want keep your branch changes or not. If you want to keep the changes what you did, delete the conflict marker they are,<<<<<<<, =======, >>>>>>> and then do a merge.

Commands to commit the changes.

git add . or git add "your_file"
git commit -m "Merge conflicts resolved"

Solving git conflict your GUI

What if you have so many merge conflicts in files? In that case you can use GUI, well its just personal choice whether to opt for GUI or not. In GUI, you can see side by side diff. It is easy for the beginners to see the changes.

You need to configure your git to use the mergetool you want to use. Example: meld, kdiff3 or vimdiff. Again it's upto the user what they prefer. I use meld. Here you need to install these tool, after installation you have to configure it.

git config merge.tool meld 

you can solve merge conflicts

git mergetool -t meld

Then you can follow the step 2 to 4 from solving using text editor, to solve the merge conflicts.

like image 79
danglingpointer Avatar answered Nov 08 '22 02:11

danglingpointer