Instructions on how to update a GitHub pull request to use the current version of the master branch.
How‑To: Rebasing GitHub Pull Requests
This is pretty esoteric but I keep forgetting how to do it so let’s put it here on the blog.
Situation
You’re maintaining a project on GitHub. A contributor has submitted a pull request. In the interim, the master branch has been updated and you want to bring the pull request up to date with the current version of master.
The best instructions I found were these ones on Stack Overflow. What I’m suggesting here is largely a simplification of what’s there so credit to Ryan Stewart for the answer.
Oh, and special thanks to Ash Maroli for being patient as I badgered him with e-mails asking for help (and who taught me the trick in step 2).
Steps
Step 1. Update upstream
git fetch upstream
This assumes you have a remote called upstream
that’s pointed at the project’s repository.1
Step 2. Checkout PR
git fetch upstream pull/<pr_number>/head:<local_branch_name>
git checkout <local_branch_name>
Step 3. Rebase PR
git rebase upstream/master
Step 4. Force Push PR
git push --force [email protected]:<contributor>/<project>.git <local_branch_name>:<remote_branch_name>
The <remote_branch_name>
is the name of the branch on the contributor’s fork of the repository. The name of this branch is visible on the PR’s page.
And that’s it. As the Stack Overflow answer points out, one should always be careful with force pushing:2
…force pushing is dangerous, and you can lose commits with it. Only use it if you’re absolutely sure you know what you’re doing, like right here, where you intentionally want to drop the old, useless commits in the pre-rebase… branch.
I hope that’s useful for someone and/or me. ✺
-
Here’s some instructions on how to add an
upstream
remote. ↩ -
As Ash pointed out over e-mail, there are ways to recover from a lost commit occurring from a wayward
git rebase
. It’s beyond the scope of this article butgit reflog
andgit reset
are what you want to stick in your search engine. ↩