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
- Open the Pull Request (PR) page on GitHub.
- Look for the “This branch has conflicts that must be resolved” message.
- GitHub will list the conflicting files.
- If the “Resolve conflicts” button is available, click it to manually resolve conflicts in the GitHub UI.
- 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
- Ensure your local repository is up to date:
git fetch origin
- Switch to the branch with conflicts (e.g.,
feature-branch
):
git checkout feature-branch
- 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");
- Resolve conflicts manually in each file.
- Save the file.
- Stage the resolved file using
git add
.
git add index.js
4. Committing and Pushing the Fix
- Commit the resolved merge:
git commit -m "Resolved merge conflicts"
- Push the updated branch to GitHub:
git push origin feature-branch
- 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
Step | Solution |
---|---|
Identify conflicts | GitHub UI or git merge origin/main |
Resolve conflicts locally | Edit the conflicting files manually |
Commit & push fixes | git commit -m "Resolved merge conflicts" → git push origin feature-branch |
Choose merge strategy | git merge (default) or git rebase (cleaner history) |