远程分支与fork分支合并_github的fork和上游项目同步的方法

具体来说步骤如下:

fork项目

在github上面fork一个项目如此简单,以至于很多人的仓库里面fork出来的项目都泛滥成灾了!以前没有找到删除fork出来的项目的方法,原来藏在setting的dangerous zone里面。

克隆项目到本地

虽然github也提供在线编辑功能,但是一般还是拉回本地更方便。这里以weld-core项目为例:

git clone https://github.com/weld/core weld-core

1

gitclonehttps://github.com/weld/core weld-core

设置上游远程仓库

当成功fork一个项目后,无论你怎么修改fork出来的项目,原来的项目(github叫做upstream,一般译作上游项目)是不会受到影响的,这在上游项目来说自然是极好的保护,但是,fork出来的项目如何能够及时反映上游项目的变更呢?这就首先需要设置本地项目的“远程仓库”属性,即告诉git命令,本地项目的上游项目是谁。比如针对weld-core项目,

git remote add upstream https://github.com/weld/core

1

gitremoteaddupstreamhttps://github.com/weld/core

设置好远程仓库后,可以检查一下:

subaochen@subaochen:~/git/weld-core$ git remote -v

originhttps://github.com/subaochen/core.git (fetch)

originhttps://github.com/subaochen/core.git (push)

upstreamhttps://github.com/weld/core (fetch)

upstreamhttps://github.com/weld/core (push)

1

2

3

4

5

subaochen@subaochen:~/git/weld-core$gitremote-v

originhttps://github.com/subaochen/core.git (fetch)

originhttps://github.com/subaochen/core.git (push)

upstreamhttps://github.com/weld/core (fetch)

upstreamhttps://github.com/weld/core (push)

现在,本地仓库有了两个远程仓库:

origin: 这是fork出来的仓库

upstream: 这是上游项目的仓库

同步远程仓库

设置好上游远程仓库后,将上游远程仓库的最新变化更新到本地。我这边的情况是很久没有和上游项目同步了,因此更新下来的内容比较多:

subaochen@debian:~/git/weld-core$ git fetch upstream

remote: Counting objects: 21775, done.

remote: Compressing objects: 100% (56/56), done.

remote: Total 21775 (delta 6109), reused 6151 (delta 6103), pack-reused 15616

接收对象中: 100% (21775/21775), 4.61 MiB | 824.00 KiB/s, 完成.

处理 delta 中: 100% (8716/8716), 完成 1102 个本地对象.

来自 https://github.com/weld/core

* [新分支] 1.1 -> upstream/1.1

* [新分支] 1.2 -> upstream/1.2

* [新分支] 2.0 -> upstream/2.0

* [新分支] 2.0.0 -> upstream/2.0.0

* [新分支] 2.0.0.Beta5-branch -> upstream/2.0.0.Beta5-branch

* [新分支] 2.1 -> upstream/2.1

* [新分支] 2.2 -> upstream/2.2

* [新分支] 2.2.0 -> upstream/2.2.0

* [新分支] 2.3 -> upstream/2.3

* [新分支] eap6.2.x -> upstream/eap6.2.x

* [新分支] master -> upstream/master

......

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

subaochen@debian:~/git/weld-core$gitfetchupstream

remote:Countingobjects:21775,done.

remote:Compressingobjects:100%(56/56),done.

remote:Total21775(delta6109),reused6151(delta6103),pack-reused15616

接收对象中:100%(21775/21775),4.61MiB|824.00KiB/s,完成.

处理delta中:100%(8716/8716),完成1102个本地对象.

来自https://github.com/weld/core

*[新分支]1.1->upstream/1.1

*[新分支]1.2->upstream/1.2

*[新分支]2.0->upstream/2.0

*[新分支]2.0.0->upstream/2.0.0

*[新分支]2.0.0.Beta5-branch->upstream/2.0.0.Beta5-branch

*[新分支]2.1->upstream/2.1

*[新分支]2.2->upstream/2.2

*[新分支]2.2.0->upstream/2.2.0

*[新分支]2.3->upstream/2.3

*[新分支]eap6.2.x->upstream/eap6.2.x

*[新分支]master->upstream/master

......

设置本地分支

首先查看一下当前有哪些分支:

subaochen@debian:~/git/weld-core$ git branch -a

* 2.0

remotes/origin/1.1

remotes/origin/1.2

remotes/origin/2.0

remotes/origin/2.0.0

remotes/origin/2.0.0.Beta5-branch

remotes/origin/HEAD -> origin/2.0

remotes/origin/master

remotes/upstream/1.1

remotes/upstream/1.2

remotes/upstream/2.0

remotes/upstream/2.0.0

remotes/upstream/2.0.0.Beta5-branch

remotes/upstream/2.1

remotes/upstream/2.2

remotes/upstream/2.2.0

remotes/upstream/2.3

remotes/upstream/eap6.2.x

remotes/upstream/master

remotes/upstream/weld-osgi-2.x

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

subaochen@debian:~/git/weld-core$gitbranch-a

*2.0

remotes/origin/1.1

remotes/origin/1.2

remotes/origin/2.0

remotes/origin/2.0.0

remotes/origin/2.0.0.Beta5-branch

remotes/origin/HEAD->origin/2.0

remotes/origin/master

remotes/upstream/1.1

remotes/upstream/1.2

remotes/upstream/2.0

remotes/upstream/2.0.0

remotes/upstream/2.0.0.Beta5-branch

remotes/upstream/2.1

remotes/upstream/2.2

remotes/upstream/2.2.0

remotes/upstream/2.3

remotes/upstream/eap6.2.x

remotes/upstream/master

remotes/upstream/weld-osgi-2.x

要确保本地代码的分支(branch)设置合适,一般设置为master:

git checkout master

1

gitcheckoutmaster

合并上游项目和本地项目

既然本地分支设置为master,有必要将上游项目的master和本地的master合并,也就是上游项目的最新变化融合到本地:

git merge upstream/master

1

gitmergeupstream/master

提交本地变更,更新origin仓库

本地的变更需要git push回origin:

git push origin master

1

gitpushoriginmaster

提交pull request

一般是到github.com去操作,另文说明。

另外,每次修改本地代码前,需要首先和上游项目同步一下:git fetch upstream,也要和origin仓库同步一下:git fetch origin,不知道有没有办法合并为一条命令?

d4c39bd54dfbfe627dbd9fe226767b2c.png

0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值