Gangmax Blog

Git: Syncing a Fork

Sync a fork of a repository to keep it up-to-date with the upstream repository. From here. This is useful when you forked a repository in GitHub and want to get the latest changes made after the fork from the original repository to your repository.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 1. Configure the remote repository.
>git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
# 2. Check the current remote repository setting.
>git remote -v
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
# 3. Fetch the branches and their respective commits from the upstream repository.
# Commits to "master" will be stored in a local branch "upstream/master".
>git fetch upstream
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 https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
* [new branch] master -> upstream/master
# 4. Check out your fork's local "master" branch.
>git checkout master
Switched to branch 'master'
# 5. Merge the changes "from upstream/master" into your local master branch.
# This brings your fork's "master" branch into sync with the upstream
# repository, without losing your local changes.
>git merge upstream/master
Updating a422352..5fdff0f
Fast-forward
README | 9 -------
README.md | 7 ++++++
2 files changed, 7 insertions(+), 9 deletions(-)
delete mode 100644 README
create mode 100644 README.md
# Not sure if it's a better choice to use "git rebase" here if you already
# have local committed changes, to keep the history info be more kindly when
# merging back to the "master" branch.

Comments