Update a forked repository when the original repository is updated

I was wondering if there is a way to update a forked repository when the original repository from which the forked repository originates is updated.  Right now, the only way I know to update my forked repositories so that they match the original is to delete my forked repository and re-fork it, which is a pain.  There has got to be a better way…

Not sure if you can do this directly with github, but you can pull the changes locally and push it to your fork.

For instance, in your local copy (cloned from your fork), add a new remote for upstream:

git remote add upstream [upstream.git]

Now, whenever there are changes, you can do 

git pull upstream [branch]
git push origin [branch]
5 Likes

Rebase the fork is one of the solutions but trying to figure out how this is helpful. For now, even I’m also doing the same delete the repo and re-fork. But rebase should work. 

Hi @allanwang . Yes, of course there is a better way for doing that.

Adding the original repo as a remote upstream is the first thing to do and you got that already.

So, first add the original repo as a remote upstream in your local forked one:

git remote add upstream [upstream.git]

Now, you can easily syncronize your forked (origin) repository with the upstream one by doing

git checkout master # Make sure you always run the following commands from the master branch
git fetch --all
git pull --rebase upstream master
git push origin master

This will rebase the upstream changes on your local forked version so the master branch git history will look exactly the same at the end.

2 Likes

Does this mean when i do git pull i can get update from the original git ?

If you’ve set it up as a remote as described above, yes.

Each branch in your local repository can be set to “track” a remote branch (from any of the configured remotes). If you call git pull without any options it will pull from there. If you want to pull from somewhere else you can either tell git pull explicitly where to pull from, or change the tracking branch (if you want to always pull from there).

Hey @PhilipPurwoko. Yes you can, but you need to specify from where you want to pull:git pull upstream master will pull from the original repo and git pull origin master will pull from the forked one