Resolving “This branch has conflicts that must be resolved” in GitHub

When merging a pull request in GitHub, you may encounter the message:
“This branch has conflicts that must be resolved.”
This indicates that the changes in your branch conflict with the target branch (e.g., main or develop), and the conflicts must be manually resolved before merging.


1. Identifying the Conflicting Files

Checking Conflicts in GitHub UI

  1. Open the Pull Request (PR) page on GitHub.
  2. Look for the “This branch has conflicts that must be resolved” message.
  3. GitHub will list the conflicting files.
  4. If the “Resolve conflicts” button is available, click it to manually resolve conflicts in the GitHub UI.
  5. After resolving the conflicts, click “Mark as resolved”“Commit merge”.

2. Resolving Conflicts Locally (Recommended)

If the conflicts cannot be resolved via GitHub UI, follow these steps to resolve them locally.

Fetch the latest changes and switch to the branch

  1. Ensure your local repository is up to date:
   git fetch origin
  1. Switch to the branch with conflicts (e.g., feature-branch):
   git checkout feature-branch
  1. Merge the target branch (e.g., main):
   git merge origin/main

If conflicts exist, Git will display a warning.


3. Manually Resolving Conflicts

When a conflict occurs, the conflicting file will include special markers like <<<<<<<, =======, and >>>>>>>.

Example of a conflict in index.js

<<<<<<< HEAD
console.log("This is from the main branch");
=======
console.log("This is from the feature branch");
>>>>>>> feature-branch

After resolving the conflict

console.log("This is the final merged version");
  1. Resolve conflicts manually in each file.
  2. Save the file.
  3. Stage the resolved file using git add.
   git add index.js

4. Committing and Pushing the Fix

  1. Commit the resolved merge:
   git commit -m "Resolved merge conflicts"
  1. Push the updated branch to GitHub:
   git push origin feature-branch
  1. Open the PR page on GitHub and verify that conflicts are resolved.

5. Choosing a Merge Strategy

merge (default)

Merges the changes while keeping the commit history:

git merge origin/main

Retains full commit history with a merge commit.

rebase (cleaner history)

Moves the changes on top of the latest main branch:

git rebase origin/main

Creates a linear commit history without merge commits.

Choose the method based on your team’s Git workflow.


Summary

StepSolution
Identify conflictsGitHub UI or git merge origin/main
Resolve conflicts locallyEdit the conflicting files manually
Commit & push fixesgit commit -m "Resolved merge conflicts"git push origin feature-branch
Choose merge strategygit merge (default) or git rebase (cleaner history)