0%

fork 别人的代码,如何更新主分支最新代码

fork 操作:就是从别的大佬远程端 git仓库 拷贝一份到自己的 git仓库

在开发学习时,经常通过 fork 别人的 git 开源项目进行开发学习。
所以在我们开发完成后,想合并到大佬的主分支去,我们需要进行创建一个 merge request, 提交给主分支代码管理员进行代码审核,审核通过后允许 merge,这样我们开发的代码就会合并到主项目中。

那么大佬的主分支有更新了,我们怎样将主分支的更新代码拉取、合并到本地呢?

fork 分支更新流程:

1. 首先要确认是否建立了主项目的远程源:

1
$ git remote -v

如果只显示自己的两个源(fetch, push)如下:

1
2
3
tonyt@xiaofeng web-poc % git remote -v
origin http://10.25.0.130/web-tools/web-poc.git (fetch)
origin http://10.25.0.130/web-tools/web-poc.git (push)

2. 则说明没有建立远程的 repo 源,需要添加主 repo 的源:

1
git remote add upstream https://github.com/b******/c*****.git    # git remote add upstream URL

这里的 upstream 是我们建立的远程 branch 的一个本地名。对于 url 一般有 https 与 ssh 的方式,如果是 ssh 方式,则需要添加 ssh 的 url,不能添加 https 的方式,否则不能在 ssh 下访问该 url。

3. 添加完之后再次使用 git remote -v 来确认。

1
2
3
4
5
tonyt@xiaofeng web-poc % git remote -v
origin http://10.25.2.87:9980/root/web-poc.git (fetch)
origin http://10.25.2.87:9980/root/web-poc.git (push)
upstream http://10.25.0.130/web-tools/web-poc.git (fetch)
upstream http://10.25.0.130/web-tools/web-poc.git (push)

有 upstream 这样就是已经创建了主 repo 的远程源。

4. 拉取主 repo 源代码:

1
$ git fetch upstream

5. 合并主 repo 代码到本地代码:

1
$ git merge upstream/master

6. 大功告成!!!!