Git fork分支后与原仓库保持同步

假设原仓库为 https://github.com/lmshao/OldHub.git
fork后的新仓库为:git@github.com:lmshao/NewHub.git

1. clone新仓库

$git clone git@github.com:lmshao/NewHub.git
Cloning into 'NewHub'...
Resolving deltas: 100% (1/1), done.
remote: Total 7 (delta 1), reused 0 (delta 0), pack-reused 0
$cd NewHub/  # 进入工程目录

2. 添加原仓库的远程链接
设置原仓库的别名为OldHub,默认的别名为origin。

$git remote add OldHub https://github.com/lmshao/OldHub.git 

查看远程仓库信息

$git remote -v
OldHub	https://github.com/lmshao/OldHub.git (fetch)
OldHub	https://github.com/lmshao/OldHub.git (push)
origin	git@github.com:lmshao/NewHub.git (fetch)
origin	git@github.com:lmshao/NewHub.git (push)

OldHub为原仓库的别名,origin指新仓库

3. 获取远程仓库内容

$git fetch OldHub   #其实获取的是仓库信息
warning: no common commits
 * [new branch]      master     -> OldHub/master

查看所有分支

$git branch -a
* master
  remotes/OldHub/master		#原仓库远程分支
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

4. Merge远程仓库分支的内容到新分支
merge的时候可以把远程分支下下来进行merge,也可以直接merge远程的分支。

方法一:下载远程仓库进行merge

$git checkout -b OldMaster remotes/OldHub/master  # 新建并切换到OldMaster分支,从远程分支下载内容到此分支
Branch 'OldMaster' set up to track remote branch 'master' from 'OldHub'.
Switched to a new branch 'OldMaster'

$git branch  #查看本地分支
* OldMaster
  master
$git checkout master  # 切换到新仓库的master分支
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$git merge OldMaster # merge原仓库的master分支
fatal: refusing to merge unrelated histories

$git merge OldMaster --allow-unrelated-histories  # merge出错的话使用此命令
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

方法二:不下载远程仓库,merge远程分支

$git merge OldHub/master  # merge原仓库的master分支

$git merge OldHub/master --allow-unrelated-histories # merge出错的话使用此命令
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

5. 推送到远程分支

$git push
or
$git push -u origin master
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值