36

I just forked a project in Github. I made modifications and sent a pull request. The owner merged my fork with the main project and after that he made some modifications. So for now my fork is not updated with the main project. I miss the modifications he made after merging my pull request. How can I update my fork with the Main project? Is there a way to do that in the web interface?

Thanks

Simon Zyx
  • 103
bAN
  • 1,776

3 Answers3

41

By design, forking a project creates a separate repo that is not updated when the original repo changes. However, git makes it pretty easy to update manually.

You need the help of a 3rd repository (your local copy suffices). There are 3 repos:

  • "Upstream": The upstream project's repository on Github.
  • "Origin": Your fork's repository on Github
  • "Local": Your local repository on your computer. I will assume you created it by cloning Fork using git clone git@github.com:your-username/projectname.git, and that everyone is using branch master.

Assuming currently "Origin" and "Local" are in the same state, and "Upstream" is ahead by 1 or more commits (the merge and any subsequent changes).

First add the upstream project as a Git remote:

git remote add upstream https://github.com/upstream-username/projectname.git

Then pull (meaning fetch and then merge automatically) the changes from the remote's master branch into your local repository's current (master) branch:

git pull upstream master

Now your local repository is in sync with upstream. Finally, push your local repo to your Github fork:

git push origin master

Now everything is in sync.

Mechanical snail
  • 7,963
  • 5
  • 48
  • 67
1

You need to add a remote (see GitHub help) and pull from that new remote.

git remote add mainProject https://github.com/user/mainProject
git pull mainProject master
VonC
  • 14,588
0

I found this answer to complement the original answer as that only deals with syncing one branch (master). In addition, if the branch has been created on the upstream since your fork, it is a little more involved to create it on your fork.

The short answer is, to update your fork with every branch on the upstream repository, execute this command.

for branch in $(git ls-remote --heads upstream|sed 's#^.*refs/heads/##'); do git push origin refs/remotes/upstream/$branch:refs/heads/$branch; done

What this does not do is remove any branches from your fork that have been deleted from the upstream. There is no way to automate that especially if you have created branches on your fork.

Brian
  • 101