Another brief ‘how to’ for those who need to pull and push to someone else’s upstream GitHub pull request (PR).

I was working on a teammate’s GitHub pull request for Project OCEAN (a current focus of mine) to help close it out. Since her pull request came from her own personal account, there were key commands I needed in order to pull and push directly to it.

Pull Existing PR

To pull an existing PR from an upstream project.

git fetch [NAME REMOTE PROJECT] pull/[PR NUMBER]/head:[PR BRANCH NAME]
Example:
git fetch upstream pull/10/head:pr_change

The command includes the pull request number like the above example, 10 and the name of the pull request like the above example, pr_change.

The command assumes your remote URL for the project is named upstream. If the remote URL has a different name assigned to it then use that instead.

This will create a branch locally called pr_change that you will be placed in once the pull is complete. Make changes into this branch to keep it clean and easier for yourself when you want to push it back up.

Push to Existing PR

To push changes/commits back to someone else’s PR in an upstream project.

git push git@github.com:[THE SOMEONE ELSE USER ID]/[PROJECT NAME].git [PR BRANCH NAME]:[LOCAL BRANCH NAME]
Example:
git push git@github.com:someoneelse/main_project_name.git pr_change:pr_change

In the command, someoneelse is where you put the name of the user’s account that the PR is from. For the project I’m working on, my teammate’s GitHub handle is amygdala and I replace someoneelse with amygdala.

Also, replace the GitHub project name main_project_name.git with the project you are working on. In my project, the name is project-OCEAN.git and that is what I use.

Make sure to include the branch name that is in the PR as well as on your computer. Check what the branch name is locally and put that in. Ideally, you are working out of the same branch name as the remote you and just need to repeat it and put a : in between.

Additional Steps in Preparation

Before pushing, I made sure my content was synced with the main branch by committing my changes and then …

Fetching the latest on upstream main…

git fetch upstream

and merging with my local branch.

git merge upstream/pr_change

Then it was ready to to push and use the command at the start of this section.

WrapUp

Here’s a little story about how to pull and push to / from someone else’s PR on a GitHub project. That’s it. Go forth and mess with everyone else’s PRs on your project.


Pull & Push to Someone else’s Upstream GitHub PR was originally published in Google Cloud - Community on Medium, where people are continuing the conversation by highlighting and responding to this story.