Fork仓库后需要同步的一种快捷方法

385 阅读1分钟

网上关于Fork仓库A之后,变成目的仓库B。此时想要将源仓库A的变更同步到仓库B,有不少方式。

本人试了多次,还是感觉下面这种方式方便快捷,下面贴出具体实操过程[1]

# 拉取Fork出来的分支
git clone Fork的分支仓库B的giturl

# 注意:进入项目根目录,执行下面操作(当前假设为 develop 分支)

# 查看所有远程库(remote repo)的远程url(只有 orgin 路径)
git remote -v
# -> origin  https://code.abc.com/new/web-b.git (fetch)
# -> origin  https://code.abc.com/new/web-b.git (push)

# 添加源分支url
git remote add upstream 源项目A的giturl

# 查看所有远程库(remote repo)的远程url
git remote -v
# -> origin  https://code.abc.com/new/web-b.git (fetch)
# -> origin  https://code.abc.com/new/web-b.git (push)
# -> upstream        https://code.abc.com/old/web-a.git (fetch)
# -> upstream        https://code.abc.com/old/web-a.git (push)

# 从源分支获取最新的代码
git fetch upstream

# 切换到主分支(若同步其他分支,切换到对应分支即可)
git checkout master

# 合并本地分支和源分支,本地库和远程的github原仓库同步
git merge upstream/master

# push到fork分支,本地的仓库提交到github
git push origin master

若最后想要去掉源分支,执行下列代码(留着那里看着闹心)

# upstream 和上面定义的源仓库名称保持一致
git remote rm upstream

# 查看所有远程库(remote repo)的远程url(现在只有 orgin 路径)
git remote -v
# -> origin  https://code.abc.com/new/web-b.git (fetch)
# -> origin  https://code.abc.com/new/web-b.git (push)

参考资料:

[1] github fork后两个仓库之间如何同步代码