How to update a pull request from a fork with Git

Published by Monica Lent 
thumbnail
git open source

If you maintain an open-source project, there’s a good chance the pull requests you receive need a little bit of work before they’re ready to merge.

Here’s a quick step-by-step guide for how to modify commits on a pull request when it’s coming from a fork of your repo.

In this guide, we’ll assume the following:

  • Your repo: github.com/your-username/your-repo-name
  • Your contributor: their-username
  • Their PR’s branch: bugfix-branch

Step 1. Add their fork as a remote

You can name the rew remote whatever you want to (in the example below it’s called “anything”, but I’ve been using the contributor’s username in practice):

# git remote add [remote-name] [email protected]/[their-username]/[your-repo-name]

git remote add anything [email protected]/their-username/your-repo-name

Think about how you usually use commands like git pull origin master, now you’ll be using commands like git pull anything master. Anywhere you were using origin will be replaced with the name of the remote.

Step 2. Fetch from the remote

This will allow you to do the next step, which is creating local branch tracking one of their branches.

# git fetch [remote-name]

git fetch anything

Step 3. Create a new branch tracking theirs

The name of your new branch doesn’t really matter.

# git checkout -b [your-new-branch] [remote]/[their-prs-branch]

git checkout -b your-new-branch anything/bugfix-branch

Step 4. Make your changes

Make whatever changes you need to do on the branch, and commit them as usual with git commit.

Step 5. Push the changes

Once you’re done, push your changes! If you rebased, don’t forget you’ll need to force push (using the -f flag).

# git push [remote-name] HEAD:[their-prs-branch]

git push anything HEAD:bugfix-branch

Now you can check e.g. on GitHub, where you should see the updated pull request. Congrats, you did it! 🎉

Want more content like this?

I don't publish often, but when I do, you'll be the first to hear about it.

    Powered By ConvertKit and a whole lotta coffee. Unsubscribe anytime.