Adding Commits to Someone Else's Pull Request

At Tighten, we do lots of open source work. We even have a whole day every week devoted to it. And, thankfully, we have many members of the open source community contributing pull requests to our projects and packages.

But occasionally we have a pull request that needs one tiny fix: an updated Travis config file, a small adjustment to the composer.json version constraints, or a typo in the readme. We could review the pull request and ask for those changes, and that's often our strategy on internal projects.

Isn't there an easier way? Can't you just add your own commits to someone else's pull request?

Yes. And it's very easy.

example git branches showing this process

How to add commits to someone else's pull request

In our example, you are Ford Prefect. You're working on the latest edition of the Hitchhiker's Guide to the Galaxy in the repo fordprefect/the-guide. A wonderful open source contributor Arthur (arthurdent) forks your repo and makes a pull request.

Arthur's pull request looks great, but he misspelled "towel" in one section as "tolwe." Open source contributors are great, but what if he doesn't ever come back to this pull request? Instead of asking him to fix it, you want to quickly push a fix yourself.

Step 1: Clone

If you haven't already, clone your own repo locally.

git clone git@github.com:fordprefect/the-guide.git

Step 2: Add a remote

In order to see the other user’s fork (and push commits to it), you'll add their fork as a remote to your local repo.

git remote add arthurdent git@github.com:arthurdent/the-guide.git

You should now see the fork as a new remote, called arthurdent, when you run git remote -v:

sites/git/the-guide on master ➜ git remote -v
  origin    git@github.com:fordprefect/the-guide.git (fetch)
  origin    git@github.com:fordprefect/the-guide.git (push)
  arthurdent        git@github.com:arthurdent/the-guide.git (fetch)
  arthurdent        git@github.com:arthurdent/the-guide.git (push)

Step 3: Fetch from this new remote

git fetch arthurdent

Step 4: Check out their branch locally

You'll need to check out the branch they used for the pull request. If you're not sure what branch they used, look on the top of the pull request (e.g., arthurdent:master). Name it something other than master locally so there's no conflict with your own master branch.

git checkout -b arthurdent-master arthurdent/master

Step 5: Commit and push

Make your changes locally, commit, then push to their remote’s head.

git commit -am "Fix how you spelt towel"
git push arthurdent HEAD:master

That's it! If all goes well, you'll see this commit show up at the bottom of the pull request.

example pull request showing new commit

Potential Issues

     ! [remote rejected] HEAD -> master (permission denied)
    error: failed to push some refs to 'git@github.com:arthurdent/the-guide.git'

If you get an error when pushing, the other user might have unchecked the box in the pull request that says “Allow edits from maintainers.” Have them check that box in the pull request view and try again.