Resolve a Merge Conflict

Git can automatically resolve code conflicts when the review branch is merged with the target branch. In some cases, however, the conflicts must be resolved manually.

On the Merge Request page, if the Merge button is replaced by the Merge Conflicts button, it indicates a merge conflict.

Git automatically resolves conflicts if different files of the target and review branches are updated before both branches are merged. Merge conflicts are reported when the same lines of the same files are updated in the review branch and the target branch before both branches are merged. Most people will use the browser-based conflict resolution tool:

  1. Open the merge request that has conflicts.
  2. Click Merge Conflicts.
    The Merge Conflicts dialog opens.
  3. Click Resolve Conflicts to open the conflict resolution editor.
    The pane on the left indicates the number of files with conflicts and lists them. It also shows the number of conflicts in each file. The pane on the right, the code editor, displays the highlighted file with conflicts shown in the left pane.
  4. Use the right arrow to go to the first (or next) conflict.
    You can use the left arrow to go to the previous conflict, if there is one. To see the differences that cause the conflict, click View Diff.
  5. To resolve the conflict, click the conflict marker, the circle next to the line numbers, to select the change to use.
    One marker will select Use Their Change, the other will select Use Our Change. The marker turns red to indicate your choice.
  6. Go through the file and resolve each conflict independently or use Resolve # Conflicts, where # indicates the number of conflicts in the file, to resolve all conflicts in a file the same way.
    Continue until there are no more conflicts. If you're not satisfied with the resolution you chose last, click Discard Resolution. To discard all your selections and start over, click Discard All.
  7. Click Update Review to commit the files to the review branch.
  8. Click Merge and push the commit to the target branch.
Files in the review branch that no longer had conflicts were merged with the target branch. No additional action is required on the Merge Request page. If you want to delete the review branch, open or refresh the Merge Request page, and click Delete Branch.

If there are too many files with conflicts or if the files with conflicts are too large, you need to manually review each conflicting file in the review branch with the code of the same file in the target branch in a text editor and resolve the conflicting lines of code. Then, you need to follow the Git commands displayed in the dialog to resolve the conflicts with the Git command line:

  1. On your computer, open the Git CLI.

  2. If you’ve already cloned the project Git repository, navigate to its directory.

    If you haven’t cloned the Git repository, clone it.

  3. Run the commands shown on the Merge Conflicts dialog.

    The commands help you resolve the conflict and mark the conflicting code lines in files.

  4. Open each file that contains conflicts in a text editor.

    Content with conflicts is marked with <<<<<<<, =======, and >>>>>>>. The lines between <<<<<<< and ======= show the code from the target branch. The lines between ======= and >>>>>>> show the code from the review branch.

  5. Review the content and update it. Remember to remove the <<<<<<<, =======, and >>>>>>> from each conflicting file before saving it.

  6. Save all files and commit them.

    Run the git status command to view the status of conflicting files.

  7. Push the commit to the target branch.

Conflicting files in the review branch are now merged with the target branch. No additional action is required on the Merge Request page.