Merge conflicts occur when git is unable to automatically merge changes from different branches. This happens when there are conflicting changes in the same places in the repository.
The causes of conflicts may include:
Multiple users editing the same files at once.
Pulling remote repository changes (git pull) that conflict with your local edits.
Merging branches (git merge) with conflicting changes made to the same lines of code.
Pushing changes (git push) to the remote repository that has commits conflicting with your local edits.
Types of conflicts:
Line update conflicts are the most common type of conflict, which occurs when the same file line is updated differently in different branches.
On the Home tab, under Your craftspace, navigate to Repositories.
Select a repository.
Under Code on the repository page, go to Pull requests.
Select a pull request and click Merge in the top-right corner.
On the pull request page, review the conflict message, for example:
Merge blocked
...
This branch has conflicts that must be resolved
filename.js
CONFLICT (content): Automatic merge failed to resolve conflict in filename.js; Manual content merge required
Click Resolve in UI.
This will open a code editor, where the conflicting sections will be marked with specials markers:
1 ...
2 Common part of the text before the conflict
3 ...
[Accept current change] [Accept incoming change] [Accept both changes]
4 <<<<<<< main
5 Changes in the current branch
6 =======
7 Changes in the incoming branch
8 >>>>>>> feature-branch
9 ...
10 Common part of the text after the conflict
11 ...
Where:
Accept current change, Accept incoming change, and Accept both changes are buttons for resolving an individual conflict.
<<<<<<< main marks the beginning of changes in the current branch.
======= is the separator between versions.
>>>>>>> feature-branch marks the end of changes in the incoming branch.
Resolve the conflicts:
To resolve an individual conflict, next to the section in question, click:
Accept current change: Accept the changes from the current branch.
Accept incoming change: Accept the changes from the incoming branch.
Accept both changes: Accept the changes from both branches.
To resolve all conflicts, at the top right, click:
Accept current: Accept all changes from the current branch.
Accept incoming: Accept all changes from the incoming branch.
Warning
In this case, all changes will be merged even if you previously resolved individual conflicts differently.
Click Mark as resolved and Commit changes.
Identify the files with conflicts by executing this command:
git status
Files with conflicts will be displayed marked with both modified.
Open the file with the conflict in the text editor.
Locate the conflict markers (<<<<<<<, =======, >>>>>>>).
Choose the conflict resolution option:
Delete the version you do not need.
Create new code to merge both versions.
After the changes are introduced, remove all conflict markers.
Save the file.
Add the updated file into the index:
git add <file_name>
Finish the merge:
git commit -m "Resolve merge conflict"
Push the changes:
git push
File structure conflicts
To resolve a conflict when a file was updated in one branch and deleted in another:
Check the conflict status:
git status
Choose the next action with the file:
To keep the file, run this command:
git add <file_name>
To delete the file, run this command:
git rm <file_name>
Finish the merge:
git commit -m "Resolve file deletion conflict"
Using merge tools
You can use graphical tools for convenient conflict resolution.
To start the configured merge tool, such as VS Code, Sublime Merge, or others, run this command:
git mergetool
Aborting the merge
To abort the merge and revert to the state before the operation started, run this command: