git - How do I update or sync a forked repository on GitHub?

ID : 81

viewed : 412

Tags : gitgithubsynchronizationrepositoryforkgit

Top 5 Answer for git - How do I update or sync a forked repository on GitHub?

vote vote


In your local clone of your forked repository, you can add the original GitHub repository as a "remote". ("Remotes" are like nicknames for the URLs of repositories - origin is one, for example.) Then you can fetch all the branches from that upstream repository, and rebase your work to continue working on the upstream version. In terms of commands that might look like:

# Add the remote, call it "upstream":  git remote add upstream  # Fetch all the branches of that remote into remote-tracking branches  git fetch upstream  # Make sure that you're on your master branch:  git checkout master  # Rewrite your master branch so that any commits of yours that # aren't already in upstream/master are replayed on top of that # other branch:  git rebase upstream/master 

If you don't want to rewrite the history of your master branch, (for example because other people may have cloned it) then you should replace the last command with git merge upstream/master. However, for making further pull requests that are as clean as possible, it's probably better to rebase.

If you've rebased your branch onto upstream/master you may need to force the push in order to push it to your own forked repository on GitHub. You'd do that with:

git push -f origin master 

You only need to use the -f the first time after you've rebased.

vote vote


Starting in May 2014, it is possible to update a fork directly from GitHub. This still works as of September 2017, BUT it will lead to a dirty commit history.

  1. Open your fork on GitHub.
  2. Click on Pull Requests.
  3. Click on New Pull Request. By default, GitHub will compare the original with your fork, and there shouldn't be anything to compare if you didn't make any changes.
  4. Click switching the base if you see that link. Otherwise, manually set the base fork drop down to your fork, and the head fork to the upstream. Now GitHub will compare your fork with the original, and you should see all the latest changes. enter image description here
  5. Create pull request and assign a predictable name to your pull request (e.g., Update from original).
  6. Scroll down to Merge pull request, but don't click anything yet.

Now you have three options, but each will lead to a less-than-clean commit history.

  1. The default will create an ugly merge commit.
  2. If you click the dropdown and choose "Squash and merge", all intervening commits will be squashed into one. This is most often something you don't want.
  3. If you click Rebase and merge, all commits will be made "with" you, the original PRs will link to your PR, and GitHub will display This branch is X commits ahead, Y commits behind <original fork>.

So yes, you can keep your repo updated with its upstream using the GitHub web UI, but doing so will sully your commit history. Stick to the command line instead - it's easy.

vote vote


Here is GitHub's official document on Syncing a fork:

Syncing a fork

The Setup

Before you can sync, you need to add a remote that points to the upstream repository. You may have done this when you originally forked.

Tip: Syncing your fork only updates your local copy of the repository; it does not update your repository on GitHub.

$ git remote -v # List the current remotes origin (fetch) origin (push)  $ git remote add upstream # Set a new remote  $ git remote -v # Verify new remote origin (fetch) origin (push) upstream (fetch) upstream (push) 


There are two steps required to sync your repository with the upstream: first you must fetch from the remote, then you must merge the desired branch into your local branch.


Fetching from the remote repository will bring in its branches and their respective commits. These are stored in your local repository under special branches.

$ git fetch upstream # Grab the upstream remote's branches remote: Counting objects: 75, done. remote: Compressing objects: 100% (53/53), done. remote: Total 62 (delta 27), reused 44 (delta 9) Unpacking objects: 100% (62/62), done. From  * [new branch]      master     -> upstream/master 

We now have the upstream's master branch stored in a local branch, upstream/master

$ git branch -va # List all local and remote-tracking branches * master                  a422352 My local commit   remotes/origin/HEAD     -> origin/master   remotes/origin/master   a422352 My local commit   remotes/upstream/master 5fdff0f Some upstream commit 


Now that we have fetched the upstream repository, we want to merge its changes into our local branch. This will bring that branch into sync with the upstream, without losing our local changes.

$ git checkout master # Check out our local master branch Switched to branch 'master'  $ git merge upstream/master # Merge upstream's master into our own Updating a422352..5fdff0f Fast-forward  README                    |    9 -------                 |    7 ++++++  2 files changed, 7 insertions(+), 9 deletions(-)  delete mode 100644 README  create mode 100644 

If your local branch didn't have any unique commits, git will instead perform a "fast-forward":

$ git merge upstream/master Updating 34e91da..16c56ad Fast-forward                 |    5 +++--  1 file changed, 3 insertions(+), 2 deletions(-) 

Tip: If you want to update your repository on GitHub, follow the instructions here

vote vote


A lot of answers end up moving your fork one commit ahead of the parent repository. This answer summarizes the steps found here which will move your fork to the same commit as the parent.

  1. Change directory to your local repository.

    • Switch to master branch if you are not git checkout master
  2. Add the parent as a remote repository, git remote add upstream <repo-location>

  3. Issue git fetch upstream
  4. Issue git rebase upstream/master

    • At this stage you check that commits what will be merged by typing git status
  5. Issue git push origin master

For more information about these commands, refer to step 3.

vote vote


If, like me, you never commit anything directly to master, which you should really, you can do the following.

From the local clone of your fork, create your upstream remote. You only need to do that once:

git remote add upstream 

Then whenever you want to catch up with the upstream repository master branch you need to:

git checkout master git pull upstream master 

Assuming you never committed anything on master yourself you should be done already. Now you can push your local master to your origin remote GitHub fork. You could also rebase your development branch on your now up-to-date local master.

Past the initial upstream setup and master checkout, all you need to do is run the following command to sync your master with upstream: git pull upstream master.

Top 3 video Explaining git - How do I update or sync a forked repository on GitHub?