Help Center/ CodeArts Repo/ User Guide/ Managing Merge Requests/ Resolving Code Conflicts in an MR
Updated on 2024-12-11 GMT+08:00

Resolving Code Conflicts in an MR

When using CodeArts Repo, you may encounter the situation where two members in the same team modify a file at the same time. Code fails to be pushed to a CodeArts Repo repository due to the code commit conflict. The following figure shows a push failure caused by the file change conflict in the local and remote repositories.

  • The returned messages vary depending on Git versions and compilers but have the same meaning.
  • The information similar to "push failure" and "another repository member" in the returned message indicates that there is a commit conflict.
  • Git automatically merges changes in different lines of the same file. A conflict occurs only when the same line of the same file is modified (the current version of the local repository is different from that of the remote repository).
  • Conflicts may occur during branch merge. The locating method and solution are basically the same as those for the conflict during the commit to the remote repository. The following figure shows that a conflict occurs when the local branch1 is merged into the master branch (due to the changes in the file01 file).

  • Max. 50 conflict files can be solved, and the size of a single conflict file cannot exceed 200 KB.

Resolving a Code Commit Conflict

To resolve a code commit conflict, pull the remote repository to the working directory in the local repository. Git will merge the changes and display the conflict file content that cannot be merged. Then, modify the conflicting content and push it to the remote repository again (by running the add, commit, and push commands in sequence).

The following figure shows that there is a file merge conflict when you run the pull command.

Modify the conflict file carefully. If necessary, negotiate with the other member to resolve the conflict and avoid overwriting the code of other members by mistake.

The git pull command combines git fetch and git merge. The following describes the operations in detail.

git fetch origin master # Pull the latest content from the master branch of the remote host.
git merge FETCH_HEAD    # Merge the latest content into the current branch.

During merge, a message indicating that the merge fails due to a conflict is displayed.

Example: Conflict Generation and Resolution

The following shows an example to help you understand how a conflict is generated and resolved.

A company uses CodeArts Repo and Git to manage a project. A function (the file01 file is modified) of the project is jointly developed by developer 1 (01_dev) and developer 2 (02_dev). The two developers encounter the following situation.

  1. file01 is stored in the remote repository. The following shows the file content.

  2. 01_dev modifies the second line of file01 in the local repository and successfully pushes the file to the remote repository. The following shows the file content in the local and remote repositories of 01_dev.

  3. 02_dev also modifies the second line of file01 in the local repository. When 02_dev pushes the file to the remote repository, a conflict message is displayed. The following shows the file content in the local repository of 02_dev, which is conflicting with that in the remote repository.

  4. 02_dev pulls the code in the remote repository to the local repository, detects the conflict starting from the second line of the file, and immediately contacts 01_dev to resolve the conflict.
  5. We find that they both modified the second line and added content to the last line, as shown in the following figure. Git identifies the content starting from the second line as a conflict.

    Git displays the changes made by the two developers and separates them using =======.

    • The content between <<<<<<<HEAD and ======= indicates the changes of the local repository in the conflicting lines.
    • The content between ======= and >>>>>>> indicates the changes of the remote repository in the conflicting lines, that is, the pulled content.
    • The content after >>>>>>> is the commit ID.
    • Delete <<<<<<<HEAD, =======, >>>>>>>, and commit ID when resolving the conflict.
  6. The two developers agree to retain all changes after discussion. After 02_dev modifies the content, the modified and added lines are saved in the local repository of 02_dev, as shown in the following figure.

  7. 02_dev pushes the merged changes to the remote repository (by running add, commit, and push commands in sequence). The following shows the file content in the remote repository after a successful push. The conflict is resolved.

In the preceding example, TXT files are used for demonstration. In the actual situation, the conflict display varies in different text editors and Git plug-ins of programming tools.

Preventing a Conflict

Repository preprocessing before code development can prevent commit and merge conflicts.

In Example: Conflict Generation and Resolution, 02_dev successfully resolves the conflict in the commit to the remote repository. For 02_dev, the latest code version of the local repository is the same as that of the remote repository. For 01_dev, version differences still exist between the local and remote repository. A conflict will occur when 01_dev pushes code to the local repository. The following describes methods to resolve the conflict.

Method 1 (recommended for beginners):

If your local repository is not frequently updated, clone the remote repository to the local repository to modify code locally, and commit the changes. This directly resolves the version differences. However, if the repository is large and there are a large number of update records, the clone process will be time-consuming.

Method 2:

If you modify the local repository every day, create a develop branch in the local repository for code modification. When committing code to the remote repository, switch to the master branch, pull the latest content of the master branch in the remote repository to the local repository, merge the branches in the local repository, and resolve the conflict. After the content is successfully merged into the master branch, commit it to the remote repository.

Resolving a Merge Conflict

CodeArts Repo supports branch management. When branches are merged, conflicts may occur. This case describes how to resolve a merge request conflict by reproducing it.

  1. Create a repo named Demo_Test.
  2. Create a file named FileTest based on the master branch. The following figure shows the content of the file.

    Figure 1 Creating the FileTest file in the master branch

  3. Create the branch_test branch based on the master branch. In this case, the content in the master branch is the same as that in the branch_test branch. The following describes how to make the content of the two branches different.
  4. In the master branch, modify FileTest as shown in the following figure, and enter the commit message Update FileTest in master.

    Figure 2 Modifying the FileTest file in the master branch

  5. Switch to the branch_test branch and modify the FileTest content, as shown in the following figure. Fill in the commit information as Update FileTest in branch_test. Now the content of the master branch is different from that of the branch_test branch and this is when a code conflict occurs.

    Figure 3 Modifying the FileTest file in the branch_test branch

  6. Switch to the branch_test branch, click Create MR in the upper right corner, and merge the branch_test branch into the master branch.

    The Details page of the merge request is displayed as shown in the following figure. Merge conflict: unresolved is displayed, and you are recommended to Fix them online or offline.

    Figure 4 Creating a merge request

  7. Perform the following operation to resolve the conflict:

    • Resolving a conflict on CodeArts Repo (recommended for small code volume)
      1. Click Fix them online and the following page is displayed. To resolve the conflict, select Apply to Source Branch or Apply to Target Branch. If you select Apply to Source Branch, the content of the branch_test branch is applied to the master branch. If you select Apply to Target Branch, the content of the master branch is applied to the branch_test branch.

      2. If there are many conflicts, click to go to the page shown in the following figure to edit or resolve them online. The lines where the <<<<, >>>>, and ==== signs are located display conflict and splitter which need to be deleted when modifying the code to solve the conflict.
        Figure 5 Solving conflicts online

    • offline (recommended for large-scale projects)

      Click offline and follow the prompted instructions as shown in the following figure.

    CodeArts Repo automatically generates Git commands based on your branch name. You only need to copy the commands and run them in the local repository.

  8. Use either of the following methods to resolve the conflict. Click Merge to merge the branches and the system displays a message indicating that the merge is successful. There is no difference in the content of the branch_test source branch and that of the master target branch.