Handling Merge Conflicts in Pull Requests
What you'll learn: How to identify and resolve merge conflicts that occur when the target branch has evolved since you opened your pull request.
The Problem: Your PR Falls Behind
You've created a pull request, but while it's awaiting review, teammates have merged their own changes into the main branch. Now when you try to merge, Git can't automatically combine the changes—you have a conflict in your PR.
This is like two editors simultaneously revising the same paragraph of a manuscript. When it's time to publish, someone needs to manually decide which edits to keep.
Resolving Conflicts in Your Pull Request
When conflicts arise in a PR, you have two main approaches:
Merge the target branch into your feature branch:
# On your feature branch
git checkout feature-analysis
git merge main
# Resolve conflicts as you've learned before
git add .
git commit -m "Resolve merge conflicts with main"
git push
Rebase your branch onto the updated target:
git checkout feature-analysis
git rebase main
# Resolve conflicts during rebase
git push --force
The merge approach preserves the full history and is safer for shared branches. The rebase approach creates a cleaner linear history but requires force-pushing (which you've learned can be risky).
Best Practice for PRs
Most teams prefer the merge approach for pull requests because it's more transparent and doesn't require rewriting public history. Your PR simply gets updated with a merge commit showing how conflicts were resolved.
Always pull the latest changes from the target branch regularly while your PR is open to minimize large conflicts at merge time.
Key Takeaway: When your PR conflicts with the target branch, merge the target into your feature branch, resolve conflicts locally, and push—keeping your PR up-to-date without rewriting shared history.