本文首发简书 http://www.jianshu.com/p/840ea273f25a

问题

github上fork之后,原始分支有改动,该如何同步原始分支呢? 还是需要google下, 作为一个小技巧!
下面就以我的 google官方android-architecture为例说明。
习惯使用【Git Bash】方式,本文就以命令行操作!

命令行操作

操作之前,先看下 fork之后github的更新提示。

提示原始分支有17个commit提交

step 1

使用GitBash进入到项目目录,简单的方式是从文件管理器中,打开GitBash, 或者是Window CMD.

window直接在目录栏输入cmd

windows 右键选择GitBash

step 2 git remote add upstream xxxx

与上游仓库同步代码之前,必须配置 remote,指向上游仓库 本文为例, 上游原始仓库为:
https://github.com/googlesamples/android-architecture.git

git remote add upstream https://github.com/googlesamples/android-architecture.git

格式类似:

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

使用如下命令查看下:git remote -v

$ git remote -v
origin  https://github.com/tancolo/android-architecture.git (fetch)
origin  https://github.com/tancolo/android-architecture.git (push)
upstream        https://github.com/googlesamples/android-architecture.git (fetch)
upstream        https://github.com/googlesamples/android-architecture.git (push)

上部分是自己远程仓库,下部分是上游仓库地址!

step 3 git checkout master

我习惯是fork的项目,一般不在主分支修改,自己另建分支, 所以, 切换到本地master分支!

git checkout master

step 4 git fetch upstream

git fetch upstream

结果如下图, 上游仓库涉及的分支比较多!

git fetch upstream

step 5 git merge upstream/master

$ git merge upstream/master
Updating 5762466..4849c04
Fast-forward
 README.md | 121 +++++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 68 insertions(+), 53 deletions(-)

step 6 git push origin master

$ git push origin master
Counting objects: 45, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (24/24), done.
Writing objects: 100% (45/45), 11.83 KiB | 0 bytes/s, done.
Total 45 (delta 26), reused 40 (delta 21)
remote: Resolving deltas: 100% (26/26), completed with 2 local objects.
To https://github.com/tancolo/android-architecture.git
   5762466..4849c04  master -> master

刷新github页面

刷新页面

要是想更新其他分支,需要执行

1. git checkout 其他分支名
2. git merge upstream/其他分支名
3. git push origin 其他分支名

步骤总结

  • 1 bash进入项目目录
  • 2 git remote add upstream 上游仓库名称.git
  • 3 git checkout master
  • 4 git fetch upstream
  • 5 git merge upstream/master
  • 6 git push origin master

参考

http://jinlong.github.io/2015/10/12/syncing-a-fork/
https://www.zhihu.com/question/20393785

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐