Resolve merge conflicts in a Git repository?

Resolving a merge conflict using the command line

To resolve merge conflicts you always need to do it manually this article will shows you that how you can resolve merge conflicts using using the command line and a text editor.

Merge conflicts error usually occurs when you try to take pull in your current branch and try to merge  one branch to another branch

Generate a list of the files affected by the merge conflict. use git status. this shows that there is a conflict in newfile.php.

$ git status
> # On branch some-branch
> # You have unmerged paths.
> #   (fix conflicts and run "git commit")
> #
> # Unmerged paths:
> #   (use "git add ..." to mark resolution)
> #
> # both modified:      newfile.php
> #
> no changes added to commit (use "git add" and/or "git commit -a")

Open your repo code in any text editor, and open your conflicted file in my case it is newfile.php
 

To see the conflict's file there be will code like the following.

The code below the  <<<<<<< HEAD shows your code and code below the ======= shows the code of other branch which means that there is conflict between these line of code. Then you need to decide which code should keep according to scenario. Some time you need to keep both line of code or some time you need latest code.

<<<<<<< HEAD
SELECT id,name,age from users
=======
SELECT id,name from users
>>>>>>> branch-a

The above code shows that you update a query, that is correct according to your  scenario so you should keep above code the new code will be look like following

SELECT id,name,age from users

Then stage your changes using git add newfile.php

final step is to commit your changes git commit -m "Resolved merge conflict by incorporating both suggestions."

 


Tags:

git

Share:

Related posts