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.
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.
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).
Step 1. Update
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@example.com:<contributor>/<project>.git <local_branch_name>:<remote_branch_name>
<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. ✺