---
title: "How to keep your fork up-to-date"
description: "Keeping the fork up-to-date is no easy task. This blog can help."
canonical_url: "https://www.bigbinary.com/blog/how-to-keep-your-fork-uptodate"
markdown_url: "https://www.bigbinary.com/blog/how-to-keep-your-fork-uptodate.md"
---

# How to keep your fork up-to-date

Keeping the fork up-to-date is no easy task. This blog can help.

- Author: Neeraj Singh
- Published: September 13, 2013
- Categories: Misc

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.

```plaintext
git clone git@github.com:neerajsingh0101/rails.git
```

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

```plaintext
origin git@github.com:neerajsingh0101/rails.git (fetch)
origin git@github.com:neerajsingh0101/rails.git (push)
```

Now I will add `upstream remote` by executing following command.

```plaintext
git remote add upstream git@github.com/rails/rails.git
```

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

```plaintext
origin git@github.com:neerajsingh0101/rails.git (fetch)
origin git@github.com:neerajsingh0101/rails.git (push)
upstream git://github.com/rails/rails.git (fetch)
upstream 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.

```plaintext
git checkout -b exception-handling
```

In the `Gemfile` I will use this code like this

```plaintext
gem 'rails', github: 'neerajsingh0101/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.

```plaintext
git checkout master
git fetch upstream
git rebase upstream/master
git 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.

```plaintext
git checkout exception-handling
git rebase master
git push -f
```

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

## Links

- [Human page](https://www.bigbinary.com/blog/how-to-keep-your-fork-uptodate)
