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:
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:
-
On your computer, open the Git CLI.
-
If you’ve already cloned the project Git repository, navigate to its directory.
If you haven’t cloned the Git repository, clone it.
-
Run the commands shown on the Merge Conflicts dialog.
The commands help you resolve the conflict and mark the conflicting code lines in files.
-
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. -
Review the content and update it. Remember to remove the
<<<<<<<
,=======
, and>>>>>>>
from each conflicting file before saving it. -
Save all files and commit them.
Run the
git status
command to view the status of conflicting files. -
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.