Git自学成才——fork仓库保持同步

栏目: 编程工具 · 发布时间: 6年前

内容简介:核心思想是利用多个远程仓库(remote repository)先执行完fork,然后到自己的仓库clone代码下来,以master分支为例

fork别人的项目到自己的repository,然后clone到本地进行开发。然后别人的项目也在继续开发,本文主要说明如何把fork的项目的新的提交同步到自己的仓库。

核心思想是利用多个远程仓库(remote repository)

先执行完fork,然后到自己的仓库clone代码下来,以master分支为例

$ git branch
 * master复制代码

保持工作目录是最新的并且是干净的

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean复制代码

接下来,看一下远程仓库

$ git remote -v
origin	ssh://git@xxx/YOUR_REPO/my-android.git (fetch)
origin	ssh://git@xxx/YOUR_REPO/my-android.git (push)复制代码

此处origin别名对应的远程仓库地指向的是自己的仓库(因为是从自己的仓库clone的)

接下来,我们要添加对于目标仓库(别人的仓库)的指向。

$ git remote add upstream ssh://git@xxx/UPSTREAM_REPO/my-android.git复制代码

然后再看一下远程仓库情况

$ git remote -v
origin	ssh://git@xxx/YOUR_REPO/my-android.git (fetch)
origin	ssh://git@xxx/YOUR_REPO/my-android.git (push)
upstream	ssh://git@xxx/UPSTREAM_REPO/my-android.git (fetch)
upstream	ssh://git@xxx/UPSTREAM_REPO/my-android.git (push)复制代码

已经关联了远程仓库了,使用sourcetree直观的看一下状态

Git自学成才——fork仓库保持同步

从图中看出,我fork之后,对方的仓库又有很多次提交,这时我的仓库里也有几次提交,通过rebase来保持同步。

rebase upstream的master分支

$ git rebase upstream/master复制代码

然后就是普通rebase的操作,如果有冲突不知道怎么解决,请参考我的另一篇“Git自学成才——rebase完整版”

完成rebase之后,看一下节点是OK的

Git自学成才——fork仓库保持同步

接下来是要执行push,如果直接push到自己的远程仓库,是被拒绝的

$ git push origin master:master
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://git@xxx/YOUR_REPO/my-android.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.复制代码

这时有两种方法:一种是删除远程分支,然后直接push;另一种是使用 git push --force 强制推送。

这里我选择第二种强推的方式,第一种方式参见我的另一篇“Git自学成才——rebase完整版”

$ git push -f origin master:master
Compressing objects: 100% (186/186), done.
Writing objects: 100% (281/281), 66.82 KiB | 11.14 MiB/s, done.
Total 281 (delta 126), reused 184 (delta 41)
remote: Resolving deltas: 100% (126/126), completed with 54 local objects.
To ssh://git@xxx/YOUR_REPO/my-android.git
 + 8aaa796...d298312 master -> master (forced update)复制代码

push成功,看一下sourcetree上面的状态也是OK的。

Git自学成才——fork仓库保持同步

至此,同步了别人仓库的最新提交,也保有了自己的提交。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hacking Growth

Hacking Growth

Sean Ellis、Morgan Brown / Crown Business / 2017-4-25 / USD 29.00

The definitive playbook by the pioneers of Growth Hacking, one of the hottest business methodologies in Silicon Valley and beyond. It seems hard to believe today, but there was a time when Airbnb w......一起来看看 《Hacking Growth》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换