0 votes
in DevOps by
What is a merge conflict in Git, and how can it be resolved?

1 Answer

0 votes
by

A Git merge conflict happens when you have merge branches with competing for commits, and Git needs your help to decide which changes to incorporate in the final merge.

Manually edit the conflicted file to select the changes that you want to keep in the final merge.

Resolve using GitHub conflict editor

This is done when a merge conflict is caused after competing for line changes. For example, this may occur when people make different changes to the same line of the same file on different branches in your Git repository.

Resolving a merge conflict using conflict editor:

Under your repository name, click "Pull requests."

In the "Pull requests" drop-down, click the pull request with a merge conflict that you'd like to resolve

Near the bottom of your pull request, click "Resolve conflicts."

 

Decide if you only want to keep your branch's changes, the other branch's changes, or make a brand new change, which may incorporate changes from both branches.

Delete the conflict markers <<<<<<<, =======, >>>>>>> and make changes you want in the final merge.

If you have more than one merge conflict in your file, scroll down to the next set of conflict markers and repeat steps four and five to resolve your merge conflict.

Once you have resolved all the conflicts in the file, click Mark as resolved.

If you have more than one file with a conflict, select the next file you want to edit on the left side of the page under "conflicting files" and repeat steps four to seven until you've resolved all of your pull request's merge conflicts.

Once you've resolved your merge conflicts, click Commit merge. This merges the entire base branch into your head branch.

To merge your pull request, click Merge pull request.

A merge conflict is resolved using the command line.

Open Git Bash.

Navigate into the local Git repository that contains the merge conflict.

Generate a list of the files that the merge conflict affects. In this example, the file styleguide.md has a merge conflict.

Git Status

Open any text editor, such as Sublime Text or Atom, and navigate to the file that has merge conflicts.

To see the beginning of the merge conflict in your file, search the file for the conflict marker "<<<<<<<. " Open it, and you'll see the changes from the base branch after the line "<<<<<<< HEAD."

Next, you'll see "=======", which divides your changes from the changes in the other branch, followed by ">>>>>>> BRANCH-NAME".

Decide if you only want to keep your branch's changes, the other branch's changes, or make a brand new change, which may incorporate changes from both branches.

Delete the conflict markers "<<<<<<<", "=======", ">>>>>>>" and make the changes you want in the final merge.

        In this example, both the changes are incorporated into the final merge:

Add or stage your changes. 

Commit your changes with a comment.

Now you can merge the branches on the command line, or push your changes to your remote repository on GitHub and merge your changes in a pull request.

Related questions

0 votes
asked Oct 30, 2022 in DevOps by Robindeniel
+1 vote
0 votes
asked Jan 9, 2021 in Git by SakshiSharma
...