备忘录:github下fork后如何同步源的新更新内容?

github下fork后如何同步源的新更新内容?

答案来源自知乎:gitlab或github下fork后如何同步源的新更新内容? - HyG cs的回答 - 知乎
https://www.zhihu.com/question/28676261/answer/44606041

开始:给fork配置远程库

首先确保你已经把仓库repository下载到本地,如若没有,请使用如下命令下载:

1
2
# 注意:yourname是你的用户名,yourrepository是你fork的仓库
git clone https://github.com/yourname/yourrepository.git

然后给fork配置远程库,使用如下命令:

1
git remote -v

查看远程状态,还未配置时类似如下:

1
2
origin	https://github.com/linjiafengyang/WeTeam.git (fetch)
origin https://github.com/linjiafengyang/WeTeam.git (push)

再确定一个将被同步到fork远程的上游仓库,使用如下命令:

1
2
# 注意:ORIGINAL_OWNER是你fork的作者用户名,ORIGINAL_REPOSITORY是源作着的仓库名
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

最后再次查看状态确认是否配置成功,如下:

1
git remote -v

成功时显示如下:

1
2
3
4
origin	https://github.com/linjiafengyang/WeTeam.git (fetch)
origin https://github.com/linjiafengyang/WeTeam.git (push)
upstream https://github.com/Liekka/WeTeam.git (fetch)
upstream https://github.com/Liekka/WeTeam.git (push)

接下来,同步fork

首先从上游仓库fetch分支和提交点,提交给本地master,并会被存储在一个本地分支upstream/master,命令如下:

1
git fetch upstream

然后切换到本地主分支(如果不在本地主分支的话)

1
git checkout master

再把upstream/master分支合并到本地master上,这样就完成了同步,并且不会丢掉本地修改的内容,命令如下:

1
git merge upstream/master

如果想更新到自己github的fork上,直接使用如下命令:

1
git push origin master

即搞定,接下来pull request与否就取决于你了。

完!

------本文结束感谢阅读------