How to add commits to someone else's pull request on GitHub
Need to make changes to a contributor's branch? Here's how to set up a remote to push your own commits to their work.
When I became an open source maintainer, I had to get up to speed quickly on the more advanced features of git and GitHub beyond pushing commits to my solo project repositories.
One of the first skills I picked up was how to add commits to a contributor's pull request. The first time I attempted it, I was terrified that I was going to ruin their work and leave a big mess that someone else would have to clean up. Thankfully it all went according to plan, and I was able to merge their PR after making some small changes!
This article outlines how I do it. Because git and GitHub are so massive, I'm sure there are probably other ways to pull it off. But this works for me!
Why would you want to do this?
Before I explain how, I think it's important to cover why you would want to add commits to someone else's branch.
Imagine that I have an open source project called Tanoak UI that lives on my GitHub at samuelsycamore/tanoak-ui
.
A contributor who goes by lindseylinden
is reading the docs one day and notices that I omitted an important piece of information that could help other users. She forks the repo to lindseylinden/tanoak-ui
and creates a new branch called fix-docs
to add this info, then submits a PR.
Her contribution is great, and I really want to use it! But my project uses Prettier for linting, and it won't build if it fails the linting check. Lindsey didn't run Prettier before committing her changes, and I can't merge her PR until this is resolved.
I ask Lindsey to complete the PR, but she never returns. Oh well! It happens. People have busy lives. I wait a couple weeks before finally deciding that I need to finish it myself.
Add commits to someone else's PR
Here's how I would finish lindseylinden
's pull request so I can merge it.
Clone the repo
If you haven't already, make sure you clone the main repo locally. For the hypothetical situation above, I would run the following command in the terminal:
git clone git@github.com:samuelsycamore/tanoak-ui.git
Add a remote
Use the git remote
command to create a connection to the contributor's fork of the main repository. You can name your remote anything you like. I usually name it after the contributor, and add "temp" to the beginning so I know to delete it later. For our hypothetical, the command would look like this:
git remote add templindseylinden https://github.com/lindseylinden/tanoak-ui.git
Validate the remote
You can run git remote -v
to see a list of all remotes you have set up. The remote for lindseylinden
's PR should look like this:
templindseylinden https://github.com/lindseylinden/tanoak-ui.git (fetch)
templindseylinden https://github.com/lindseylinden/tanoak-ui.git (push)
Fetch from the remote
Now that you're connected to the contributor's fork, you can use git fetch
to pull their version of the repo to your local machine. For lindseylinden
's PR, that would look like this:
git fetch templindseylinden
Checkout the contributor's branch
After you've fetched the contributor's repo, you'll need to find the branch where their work is located. You can usually find the name of the branch at the top of their pull request—in the screenshot below, my branch is named system-revisions
:
When you've found the right branch, you'll need to create your own parallel version of it using git checkout -b
. Again, you can give this any name you want. I usually name it {{ContributorName}}-{{BranchName}}, so for lindseylinden
's PR I would do this:
git checkout -b lindseylinden-fix-docs lindseylinden/fix-docs
...where lindseylinden-fix-docs
is the name of my branch that is a clone of Lindsey's lindseylinden/fix-docs
branch.
Add your commits and push
Make the necessary changes, then use git add
, git commit
, and git push
to add your changes to the contributor's branch. For Lindsey's branch, the commands would look like like this:
git add .
git commit -m "ran prettier to clean up formatting"
git push lindseylinden-fix-docs HEAD:fix-docs
If you've never run a git push
that looks like this before, essentially what that says is that I want to push my branch lindseylinden-fix-docs
directly to the contributor's fix-docs
branch.
Assuming everything went the way I intended it, I should see my commit(s) at the bottom of lindseylinden
's pull request.
Remove the remote
This isn't required per se, but if you do enough of these eventually you'll accumulate a long list of old stale remotes when you run git remote -v
.
After the work is complete and the PR has been merged, you can go ahead and delete the remote that you set up to connect to the contributor's branch.
To do this, run git remote rm
followed by the name of the remote. In my example, this would be:
git remote rm templindseylinden
As mentioned above, I like to add "temp" to the names of my remotes to remind myself that they can be safely deleted once they're no longer in use.
Potential problems
The most common error you'll encounter when trying to push to a contributor's branch looks like this:
! [remote rejected] HEAD -> fix-docs (permission denied)
error: failed to push some refs to 'https://github.com/lindseylinden/tanoak-ui.git'
This indicates that the contributor unchecked the box that on the pull request form that says Allow edits from maintainers. In order to get around this, you'll have to have the contributor edit the PR and check that box. This might be easier said than done if they've completely abandoned their work... Good luck out there, OSS maintainers! 🙃👍
If you enjoyed this piece and you want to keep up with my work, you can sign up for my email newsletter through this link. I promise to make every email something you will be excited to open!
Did you find this article valuable?
Support Sam Sycamore by becoming a sponsor. Any amount is appreciated!