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).
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 conflicting 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 conflicting file carefully. If necessary, negotiate with the other member to resolve the conflict and avoid overwriting the code of other members by mistake.
git pull 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.
- file01 is stored in the remote repository. The following shows the file content.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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 on the Console
CodeArts Repo allows you to manage branches. The following simulates a conflicting MR and describes how to resolve it.
- Create a repository.
- Create a file named file03 on the master branch in the repository. The initial content is as follows:
- Create a branch named branch007 based on the master branch.
The content in the master branch is the same as that in branch007. The following describes how to make them different.
- In the master branch, modify file03 as shown in the following figure, and enter the commit message modify in master.
- Switch to branch007, modify file03 as shown in the following figure, and enter the commit message modify in branch007. Then the two branches are different, that is, a conflict occurs.
- Create an MR to merge branch007 to the master branch. Click Create Merge Request to submit the MR.
Merge request details page is displayed. You can also click the name of the merge request in the merged requests list to access this page. Merge conflict: unsolved displays on the details page. You are advised to Fix them online or offline.
- Perform the following operation to resolve the conflict:
- Fix them online (recommended for small code volume)
- Click Fix them online. The following page is displayed, showing the code conflict.
- If the conflict cannot be resolved by overwriting the file, click to go to the Manual Editing page, as shown in the following figure. The conflict display format is similar to that in Example: Conflict Generation and Resolution.
- Manually modify the code to resolve the conflict and commit the changes.
Enter a commit message.
In the preceding figure, the following signs are used for conflict display and separation: <<<<, >>>>, and ====. Delete the lines where the signs are located when modifying code.
- Click Fix them online. The following page is displayed, showing the code conflict.
- offline (recommended for large-scale projects)
Click offline. The following page is displayed. Perform the operations as prompted.
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.
- Fix them online (recommended for small code volume)
- After the conflict is resolved by using either of the preceding methods, you can click Merge to merge branches. The system displays a message indicating that the merge is successful.
You can also follow the instructions in Managing MRs.
Now, the content of the master and branch007 branches is the same. You can switch between branches to check the content.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot