在github 上 fork 了一个优秀的项目之后,过了一段时间后,原项目作者更新了项目。此时,如何使自己 fork 的项目和原先作者的项目分支保持同步呢?
查看远程的版本库地址
$ git remote -v origin /JohnnyHL/SpringBootStudy.git (fetch)origin /JohnnyHL/SpringBootStudy.git (push)
然后,clone fork出来的仓库到本地
$ git clone git@:XXXXXXX/SpringBootStudy.git
添加原项目 git 地址到本地版本库
$ git remote add upstream /JohnnyHL/SpringBootStudy.git
检查版本库是否添加成功
$ git remote -vorigin /JohnnyHL/SpringBootStudy.git (fetch)origin /JohnnyHL/SpringBootStudy.git (push)upstream /JohnnyHL/SpringBootStudy.git (fetch)upstream /JohnnyHL/SpringBootStudy.git (push)
原项目更新内容同步到本地
$ git fetch upstream remote: Counting objects: 360, done.remote: Compressing objects: 100% (148/148), done.remote: Total 360 (delta 158), reused 339 (delta 137), pack-reused 0Receiving objects: 100% (360/360), 87.95 KiB | 45.00 KiB/s, done.Resolving deltas: 100% (158/158), completed with 66 local objects.From /JohnnyHL/SpringBootStudy.git* [new branch] master_test1-> upstream/master_test1* [new branch] master -> upstream/master
查看本地分支
$ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/master_test1remotes/origin/master remotes/upstream/master_test1remotes/upstream/master
在本地实现与upstream的同步(同步更新内容到本地对应分支)
$ git rebase upstream/master
将本地仓库更新的内容push到自己的远程仓库
git push