码云、GitHub如何更新fork后的代码

码云、GitHub上有个很方便的功能叫fork,将别人的工程一键复制到自己账号下。这个功能很方便,但有点不足的是,当源项目更新后,你fork的分支并不会一起更新,需要自己手动去更新(并且因为要保留自己修改后的代码,更不可使用强制更新的方法)。

命令行方法

1、在本地装好Git客户端,或者GitHub客户端

2、clone 自己的fork分支到本地,可以直接使用github客户端,clone到本地,如果使用命令行,命令为:

git clone [email protected]:Agent_ZhenyuanLiu/AllAgent.git

3、增加源分支地址到你项目远程分支列表中(此处是关键),先得将原来的仓库指定为upstream,命令为:

git remote add upstream https://github.com/被fork的仓库.git

此处可使用git remote -v查看远程分支列表

4、fetch源分支的新版本到本地

[master]> git fetch upstream

5、合并两个版本的代码

[master]> git merge upstream/master

6、将合并后的代码push到github上去

[master]> git push origin master
官方解决办法:
git fetch upstream
# Fetches any new changes from the original repository
git merge upstream/master
# Merges any changes fetched into your working files

GitHub站内操作

  1. 打开自己的仓库,进入code下面。
  2. 点击new pull request创建。 
    git clone [email protected]:Agent_ZhenyuanLiu/AllAgent.git
  3. 选择base fork
    git remote add upstream https://github.com/被fork的仓库.git
  4. 选择head fork
    [master]> git fetch upstream
  5. 点击Create pull request,并填写创建信息。
    [master]> git merge upstream/master

6. 点击Merge pull request 合并从源fork来的代码。

[master]> git push origin master
官方解决办法:
git fetch upstream
# Fetches any new changes from the original repository
git merge upstream/master
# Merges any changes fetched into your working files

7. 完成。

扫描二维码关注公众号,回复: 4209979 查看本文章

本文部分内容借鉴自:

http://www.cnblogs.com/zyumeng/p/3442263.html

https://blog.csdn.net/qq1332479771/article/details/56087333

https://help.github.com/articles/fork-a-repo

http://www.shizuwu.cn/post/669.html

http://segmentfault.com/q/1010000000095921

http://my.oschina.net/luffyke/blog/70336

猜你喜欢

转载自blog.csdn.net/tyyking/article/details/83583792