When we want to contribute our own code to an open source repository on Github, we usually first Fork the open source code repository, then go clone
to the local, submit our own changes, and finally Pull Request to merge.
Since open source repositories usually have many people contributing code together, to continue to collaborate with these project contributors, we will encounter a very common problem: how to keep in sync with the source repository?
Follow the steps below to keep in sync with Fork's source warehouse.
Configure remote warehouse
-
First check the currently configured remote warehouse, which
YOUR_USERNAME
is your Github username andYOUR_FORK
Fork's warehouse name.$ git remote -v > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
-
Configure the upstream warehouse to be synchronized (that is, the source warehouse of Fork), which
ORIGINAL_OWNER
is the user name of the source warehouse author and the nameORIGINAL_REPOSITORY
of the source warehouse.$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
-
Verify the configuration, the
upstream
beginning is the remote warehouse$ 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)
Synchronize fork branch
-
Obtain branches and their respective commits from upstream repositories. For
BRANCHNAME
submission will be stored in the local branchupstream/BRANCHNAME
in.$ 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] main -> upstream/main
-
Switch the local default branch, which is the
main
branch in this example .$ git checkout main > Switched to branch 'main'
-
upstream/main
Merge changes from the upstream default branch-in this case -to the local default branch.$ git merge upstream/main > 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
This operation will synchronize the local default branch with the upstream repository without losing local changes.
If you don't have any separate commit locally, Git will directly perform the "fast-forward" operation:
$ git merge upstream/main > Updating 34e91da..16c56ad > Fast-forward > README.md | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-)
Push synchronization
The above steps only synchronize the local copy of the warehouse. To synchronize updates on GitHub, you also need to push local changes to the Github online warehouse:
git push origin main
Sometimes, pushing a local copy to an online warehouse will encounter the following error:
$ git push origin main
To https://github.com/YOUR_USERNAME/YOUR_FORK.git
! [rejected] main -> main (fetch first)
error: 推送一些引用到 'https://github.com/YOUR_USERNAME/YOUR_FORK.git' 失败
提示:更新被拒绝,因为远程仓库包含您本地尚不存在的提交。这通常是因为另外
提示:一个仓库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更
提示:(如 'git pull ...')。
提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。
This means that your remote warehouse has submissions that are not included in the local copy. You only need to perform the following operations:
$ git pull origin main