Skip to content

Instantly share code, notes, and snippets.

@scottyhq
Created August 10, 2020 18:25
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottyhq/299e4d36018a2f13acfb2528a1553002 to your computer and use it in GitHub Desktop.
Save scottyhq/299e4d36018a2f13acfb2528a1553002 to your computer and use it in GitHub Desktop.
GitHub PR Merge Conflict Resolution with Rebase

How to rebase a pull request to fix merge conflicts

It's quite common to open up a pull request on GitHub and be confronted with the message This branch has conflicts that must be resolved. This situation arises when you create a feature branch on an older commit from the master branch. Maybe you forgot to run git pull master before git checkout -b geocoding_vignette or maybe a collaborator changed some of the same files on GitHub while you've been working on new things. There are many ways to fix this. One is using the Web Editor build into GitHub and fixing conflicts by hand. This works great if there are not too many conflicts.

Another technique is to rebase your pull request onto the master branch (Move your additional commits on top of the most recent master commit). This is conceptually clean, but sometimes confusing in practice to do cleanly. This example walks through the process where you want to do a rebase, and resolve conflicts by overwriting whatever is on the master branch with changes from your feature branch. Here are some helpful articles to understand more about this technique https://github.com/edx/edx-platform/wiki/How-to-Rebase-a-Pull-Request, https://howchoo.com/g/njcyntcxmwq/git-merge-conflicts-rebase-ours-theirs.

1) Make sure you have the most recent master branch locally

git checkout master
git pull
git status

Output:

On branch master
Your branch is up to date with 'origin/master'.

2) Make sure you have the most recent PR branch locally

gh pr checkout 66

Output:

From https://github.com/DSSG-eiCompare/eiCompare
 * [new branch]      geocoding_vignette -> origin/geocoding_vignette
Switched to a new branch 'geocoding_vignette'

3) Rebase your changes

git rebase master

Output:

First, rewinding head to replay your work on top of it...

Auto-merging vignettes/geocoding.Rmd
CONFLICT (content): Merge conflict in vignettes/geocoding.Rmd
error: Failed to merge in the changes.

4) Fix conflict by keeping changes from the PR feature branch

git checkout vignettes/geocoding.Rmd --theirs
git add vignettes/geocoding.Rmd
git rebase --continue
git status

Output:

On branch geocoding_vignette
Your branch and 'origin/geocoding_vignette' have diverged,
and have 125 and 7 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

5) Force push your changes to the PR feature branch on GitHub

git push -f
@NourBasha
Copy link

This was helpful, thank you.

@developerjamiu
Copy link

Thank you for this. It was really helpful

@adityandar
Copy link

Thanks, really helpful.

@Saga4
Copy link

Saga4 commented May 30, 2023

What if I dont want to do force push using "git push -f"?
Is there any way we can avoid force push here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment