Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Github - Push A Single Change Upstream (Pull Request)

After forking a repository on Github, if you make a change that could benefit the original source, then you need to create a "pull request". However, you may not want to push them all of your changes, just one of the latest ones. To solve this issue, follow the steps below.

Terminology

  • The original source repository can be referred to as the "upstream" repository, just as the source of a river is always upstream.
  • It's called a "pull" request because the owner of the upstream repository will be "pulling" code from you if they choose to accept the request. Normally when you make changes, you are "pushing" code to your own repository, but you are not allowed to "push" to someone else's repository, otherwise people could just ruin your codebase!

Steps

Firstly, make sure all of your changes have been committed and pushed to your own repository so that you don't lose anything. Now we need the full hash of the commit that you want to push upstream. This is most easily done by using git log in the cli and looking at the commit messages, so hopefully you were descriptive!

Now run the following commands, substituting $UPSTREAM_REPO AND `$COMMIT_HASH

git remote add upstream $UPSTREAM_REPO
git remote update
git checkout -b upstream upstream/master
git cherry-pick $COMMIT_HASH

At this point there are likely to be conflicts. If so, open up the file and resolve them before running git add /path/to/file to mark each file as resolved before finally running git commit.

Now run:

git push origin upstream

You will now have a branch called "upstream" which should be exactly the same as the upstream's repository, plus the single change that you "cherry picked".

Create A Pull Request On Github

  • Navigate to your own repository and click on "Pull requests".
  • Click "New pull request".
  • Select the upstream repo/branch on the left, and your own repository and "upstream" branch on the right as shown below: selection of repos/branches

  • Click "Create pull request"

  • Fill in the message dialogs and submit.

You should now have a pull request in Github, and hopefully the upstream owner will review and merge it.

References

Last updated: 9th May 2020
First published: 16th August 2018