首先需要添加主仓库为本地的远程仓库。
配置远程仓库
1、在本地项目的目录下,查看已配置的远程仓库
$git remote -v
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
可以看到已经配置了一个名为origin的远程仓库。这个就是我们fork的远程仓库。
2、配置主仓库为远程仓库
$git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
配置主仓库名为upstream的远程仓库。
其中upstream为上游仓库。
3、验证主仓库是否添加成功
$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)
同步主仓库到fork仓库
1、下载主仓库的代码到本地
$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
2、切换到fork仓库的本地代码
$git checkout master
3、合并下载到本地的主仓库的代码到fork仓库的本地代码
$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
这个是不会更改本地的修改。
参考及代码来源:
https://help.github.com/articles/configuring-a-remote-for-a-fork/
https://help.github.com/articles/syncing-a-fork/