Updating forked Github repos

Here is a helpful beginners tip about managing forked repos in Github.

When you fork a Github repo, it makes a copy of that code for you in a remote location. Often developers work by copying that remote copy to their local machines (called ‘cloning’), and then pushing new changes from their clone to their forked repo. When they are happy, they submit a pull request from their forked repo to the upstream repo and that is where their changes are merged in.

However, what happens when changes are made by other developers to that upstream repo? Your forked repo becomes out of date. If you were looking at a timeline, your copy would be set to to a point of time in the past.

Have you ever wondered how to update your forked repo with the latest changes from its upstream repo? Say you forked a repo and then it sat for 1 month without you making any changes to it. During that time, changes were made to the upstream repo. How do you bring them in sync? Here’s how I do it. The following are Git commands that can be called using Git Bash:

  1. Start by cloning your forked repo. You may have already done this after you forked the repo. If that is the case, you would have a folder on your machine named <repo name>. Using git bash, if you type “git remote –v” from within your repo, you will see a remote named ‘origin’ with a URL of your forked repo.git clone <url to forked repo>
  2. Next, move into the cloned repo.cd <repo name>
  3. Next, add a remote for the URL of the upstream copy of the repo. A remote is simply a bookmark to a URL. It’s useful so you don’t have to type out long URLs each time you want to engage a remote repo.git remote add upstream <url to upstream repo>
  4. Check to make sure that you have both remotes named ‘origin’ and ‘upstream’. Each should have a push and pull URL:git remote --v
  5. Next, pull the latest changes from the upstream remote into your cloned copy of the repo:git pull upstream master
  6. Finally, push those changes up into your forked copy. Remember: you cloned your forked repo. So the ‘origin’ remote is pointing to that repo. You may want to ask yourself “Don’t I need to add or commit these changes?” However the answer is “No”, because these are not *your* changes. They are simply going to be forwarded.git push origin

Hopefully this practice makes more sense now?

In summary, the upstream changes were pulled down into your cloned repo, and then pushed up to your forked repo to bring them in sync.

Advertisements
About

Proud Canadian who loves running, craft beer, World travel, culture and music of all shapes & styles. I live in Toronto and love this city for how diverse and unique it is. As a Solution Architect with IBM Canada's Interactive Experience (iX&M) practice, I have the privilege of leading teams of amazingly talented people to build creative and award-winning mobile applications & solutions. I love to build things and transform chaos into awesome.

Tagged with: ,
Posted in Tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: