This guide describes merge conflicts, and how to resolve a simple merge conflict on GitHub.
Prerequisites
Before you proceed complete the following:
- Install Visual Studio Code
- Install Docs Authoring Pack
- Install Word Count extension
- Install Pull Requests extension
- Install Azure Repos extension
- Install Git client
- Set Git credentials
- Terminology and concepts
- General project workflow
- Download course files (clone repo)
- Project branching policies
- Create new branch in VSC
- Send (push) files
- Markdown syntax guide
- Add/ edit markdown in VSC
- Add/ edit images in VSC
- Fix linter issues
- Pull requests overview
- Create pull request
- Work in a pull request
- Merge a pull request
Topics in this guide
An overview of merge conflicts
Git users can work in the same file on different branches concurrently. For example, two Git users working on different branches can edit line number 100 in the file example .md, at the same time, and then push their edited files “up” to GitHub.
Merge conflicts arise from attempts to merge branches that contain incompatible versions of the same file. Staying with the previous example, there is different content on line number 100 in the file example .md on each of the two users’ branches. Pushing the files to GitHub creates two competing versions of the file example .md. When you try to merge the branches, Git doesn’t know which version of the file to merge. The result is an error called a “merge conflict”.
About complex and simple merge conflicts
Resolving a merge conflict requires “human intervention” to tell Git which version of the file to merge. All merge conflicts must be resolved before GitHub will allow you to merge a pull request (PR). On the GitHub Pull requests tab, if you attempt to merge a PR with conflicting files, GitHub displays a warning message, lists the conflicting files, and deactivates the Merge pull request button. The Merge pull request button remains deactivated until you’ve resolved the merge conflicts.
Note: For more information about merge conflicts, refer to the GitHub documentation page About merge conflicts.
There are many causes of merge conflicts. For complex merge conflicts, you must identify the cause of the conflict and implement an appropriate solution. Resolving complex merge conflicts is beyond the scope of this guide. To resolve a complex merge conflict, refer to the GitHub documentation page Resolving a merge conflict using the command line.
Competing line changes, like in the previous example, are a common cause of simple merge conflicts. To resolve a simple merge conflict, caused by competing line changes, follow the steps in Resolve a simple merge conflict on GitHub.
Resolve a simple merge conflict on GitHub
-
In a web browser, sign into GitHub, and go to the project’s GitHub repository (remote repo).
For example, in the following image, the repo (test-repo) is open on GitHub.
-
Select the Pull requests tab.
-
From the list of open PRs, choose the PR with the merge conflict to be resolved.
-
In the PR Conversation tab, scroll down, and then select the button Resolve conflicts.
Note: If the button Resolve conflicts is deactivated, the merge conflict cannot be resolved on GitHub. To resolve the conflict, refer to the GitHub documentation page Resolving a merge conflict using the command line.
-
The GitHub editor opens the conflicting file on the line that contains the first conflict (a “line conflict”).
In the following markdown syntax example, like in the GitHub editor panel, the conflicting branches are named, and the conflicting line content is enclosed by the conflict markers
<<<<<<< ======= >>>>>>>
.<<<<<<< conflicting branch name 1 conflicting line on branch 1 ======= conflicting line on branch 2 >>>>>>> conflicting branch name 2
For example, in the following image, the conflict markers indicate where the line conflict occurs between the branch edit-m02-l02 and the branch master, in the file A0001-Mod02-L01-T01_example .md.
Determine which of the conflicting branches has the line content you want to keep. Alternatively, make a new change that incorporates the line content of both conflicting branches by typing into the GitHub editor directly.
-
Apply your changes in the GitHub editor.
-
Delete the conflict markers from around the changes you applied.
Note: Repeat steps 4 to 7 to resolve each line conflict in the file. Navigate between conflicting lines within a file by selecting Prev and Next.
-
When you’ve resolved all of the line conflicts in the file, choose Mark as resolved.
Note: Repeat steps 4 to 8 to resolve all conflicts in every conflicted file in the PR. The conflicted files are listed to the left of the GitHub editor panel.
-
When you’ve resolved all conflicts, select Commit merge to merge the updated branch.
Note: Your changes will be committed to the repo’s head branch (usually the master branch).
Depending on your branch policy, and the original PR configuration, you might be prompted to choose a branch to commit your changes to. If you can’t or don’t want to commit your changes to the head branch directly, select the option to Create a new branch for this commit…, then enter a branch name, and choose Create branch and update my pull request. GitHub will update the base branch for the PR you’re merging accordingly.
-
In the Conversation tab for the PR, scroll down to the message This branch has no conflicts with the base branch, and verify that the option is enabled to Merge pull request.
Note: If GitHub indicates that there are merge conflicts, repeat steps 2 to 9 in this guide until all conflicts are resolved, and then try merging the PR again.
-
With the Merge pull request option enabled, merge the PR as normal by following the guide Merge a pull request.
You have resolved a simple merge conflict on GitHub successfully.
Appendices
Check the following supplementary Appendices for more details and context.