How to keep your fork up-to-date

Neeraj Singh

By Neeraj Singh

on September 13, 2013

Let's say that I'm forking repo rails/rails. After the repo has been forked to my repository I will clone it on my local machine.

1git clone git@github.com:neerajdotname/rails.git

Now cd rails and execute git remote -v . This is what I see.

1origin git@github.com:neerajdotname/rails.git (fetch)
2origin git@github.com:neerajdotname/rails.git (push)

Now I will add upstream remote by executing following command.

1git remote add upstream git@github.com/rails/rails.git

After having done that when I execute git remote -v then I see

1origin git@github.com:neerajdotname/rails.git (fetch)
2origin git@github.com:neerajdotname/rails.git (push)
3upstream git://github.com/rails/rails.git (fetch)
4upstream git://github.com/rails/rails.git (push)

Now I want to make some changes to the code. After all this is why I forked the repo.

Let's say that I want to add exception handling to the forked code I have locally. Then I create a branch called exception-handling and make all your changes in this branch. The key here is to not to make any changes to master branch. I try to keep master of my forked repository in sync with the master of the original repository where I forked it.

So now let's create a branch and I will put in all my changes there.

1git checkout -b exception-handling

In the Gemfile I will use this code like this

1gem 'rails', github: 'neerajdotname/rails', branch: 'exception-handling'

A month has passed. In the meantime rails master has tons of changes. I want those changes in my exception-handling branch. In order to achieve that first I need to bring my local master up-to-date with rails master.

I need to switch to master branch and then I need to execute following commands.

1git checkout master
2git fetch upstream
3git rebase upstream/master
4git push

Now the master of forked repository is in-sync with the master of rails/rails. Now that master is up-to-date I need to pull in the changes in master in my exception-handling branch.

1git checkout exception-handling
2git rebase master
3git push -f

Now my branch exception-handling has my fix on top of rails master.

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.