合并两个git仓库,并保存所有的提交记录

Git是一个团队之间工作的非常好的代码备份工具,支持下载、更新等功能,但是如果遇到有两个git工程,他们都各自的提交记录,我们想合并他们的提交记录,这个时候怎么做呢。那么接下来就是见证奇迹的时刻:

首先,我描述一下这个情景如下:

  1. 有2个git仓库:g1、g2;
  2. 想将g1中的文件移入g2;
  3. g1和g2的历史日志都要要保留;

首先,准备好2个git仓库。

然后,需要五步命令:

# 1、将g1作为远程仓库,添加到g2中,设置别名为other

[jot@myhost g2]$ git remote add other ../g1/

# 2、从g1仓库中抓取数据到本仓库
[jot@myhost g2]$ git fetch other
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../g1
 * [new branch]      master     -> other/master
# 3、将g1仓库抓去的master分支作为新分支checkout到本地,新分支名设定为newbench
[jot@myhost g2]$ git checkout -b newbench other/master
Branch newbench set up to track remote branch master from other.
Switched to a new branch 'newbench'
# 4、切换回g2的master分支
[jot@myhost g2]$ git checkout master
Switched to branch 'master'
# 5、将newbench合并入master分支
[jot@myhost g2]$ git merge newbench --allow-unrelated-histories
Merge made by recursive.
 newbench.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 repo1.txt

目前为止,如果没有冲突的话,您就应该可以看到多有的提交日志了。

如果有冲突,先解决完冲突,再重新操作一遍。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值