文/onestark(简书作者) 原文链接:http://www.jianshu.com/p/29775d91f536 著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
Fork 就是远程端上一份别人仓库的拷贝。
流程:fork 源仓库 ---① fork--> fork 仓库副本 ---② clone--> local 仓库 当你在远程端(如 Github)上 fork 了别人的一个仓库时,你的远程仓库将新建一份 fork 来的“仓库副本”。如果你想在本地修改这份副本仓库,你需要先 clone 它到本地: $ git clone git@github.com:YOUR_USERNAME/YOUR_FORK# 或 $ git clone https://github.com/YOUR_USERNAME/YOUR_FORK
现在你已经有了一份 fork 的本地副本,同时你可以开始在本地修改代码了。
同步更新
流程:fork 源仓库 ---① fetch & merge--> local 仓库 ---② push--> fork 仓库副本
到目前为止,你本地仓库的远程信息可以用 $ git remote -v 查看到: https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
为了获得 fork 源仓库的更新,现在要添加 fork 源仓库的地址,例如: $ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git
这里的 upstream 是可以修改名称的,代表 fork 源仓库的别名。 用 $ 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_REPO.git(fetch) upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git (push)
以上设置好了,当你要更新 fork 源仓库的时候,首先 fetch 一份源仓库变动到本地,这回生成一个分支 upstream/master : $ git fetch upstream
可以用查看分支命令查看所有分支: $ git branch
然后,切换到本地 master 分支: $ git checkout master
合并 upstream/master 分支的变化到本地 master 分支: $ git merge upstream/master
到这里,仅把 fork 源仓库更新到了本地仓库,如果想要更新远程端的 fork 仓库副本 ,必须向远程端 push 一次: $ git push origin master
以上,fork 源仓库、fork 仓库副本 和 local 仓库实现了同步更新。
|