Going upstream your Github Fork with Sourcetree

If you ever want to have a cool Git client that visualises branches nicely and has tons of features AND costs nothing, Sourcetree is the app to have! It has multi platform support so both you Windows ánd Mac developers can enjoy it’s clean interface. BTW: I’m not affiliated in any way, just a big fan of this free app!

One of the more advanced Git uses is forking a repository. After committing on ore more pull-requests to help the original developer(s) enhance functionality, support or fix bugs you end up with a fork that is lying around.

After this contribution to a better world, you have 3 options:

  1. Keep it in your account and let it collect dust
    not handy if you have lotsa repo’s yourself or if you help out other repo’s actively.
  2. Remove the forked repo
    Do this in your Github (or BitBucket, or…) account to clean up your list of repo’s. If your pull-request is merged the use would be gone anyway
  3. Keep it in your account and update it regularly so you can maintain it and help out in the future
    That’s the spirit! Keep contributing and saving the world from disaster, you superhero you!

This post will dive into option 3 and tell you what the “upstream” is and how to maintain your fork using Sourcetree.

Good choice

[ad name=”Large Rectangle”]

More after the break..

So, here we are with our forked repo from an useful project and we want to keep it up to date.
In order to check with the original repo you want to walk upstream to find the source this river of knowledge. Hence the term upstream.

upstream

What you basically want to do is create another remote url that your current repo can pull from. To be compliant with the concepts of git as explained in the how to fork a repo post you’d call it “upstream” but you can give it any name you want as long as it’s different from the “origin” url name.

Using the Git CLI tool you could create the origin using a lot of commands and mambo jumbo but to keep this post plain and simple – just like the SourceTree app – we will focus on adding the upstream origin using the SourceTree GUI using the following steps (by Aaron Brager on SO) :

First, set up the parent repo:

  1. Open your forked repo in SourceTree.
  2. Select Settings in the toolbar.
  3. In the “Remotes” pane, press “Add”.
  4. Enter any name you like (often upstream) and the URL / path to the parent repo.
  5. Press OK, then OK.

This will result in your repo configuration having 2 origins: “origin” and “upstream”.

Bringing your forked repo back to the future is then as easy as pulling the upstream origin to update your forked repo’s code and injecting that back in your fork using a push.
It is VERY IMPORTANT that you make sure that all your work has been committed pushed before you start updating your repo. Shelving your current working branche code IS possible but it ISN’T RECOMMENDED since the new logic from the upstream might break all kinds of logic..

The steps to update your forked repo with code from the upstream using SourceTree is as easy as:

  1. Select Pull in the toolbar.
  2. In the “Pull from repository” dropdown, switch from your fork to the upstream repo you just added.
  3. Press OK.
  4. (Optional) Once you pull, you may want to Push, to push the new content up to the server.
    This will actually update your code in your forked repo on the GIT server*

*: Don’t forget to make sure that your logic still works as intended before pushing the new code.

Now, be happy and enjoy!

Excited