Git是一个团队之间工作的非常好的代码备份工具,支持下载、更新等功能,但是如果遇到有两个git工程,他们都各自的提交记录,我们想合并他们的提交记录,这个时候怎么做呢。那么接下来就是见证奇迹的时刻:
首先,我描述一下这个情景如下:
- 有2个git仓库:g1、g2;
- 想将g1中的文件移入g2;
- 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
目前为止,如果没有冲突的话,您就应该可以看到多有的提交日志了。
如果有冲突,先解决完冲突,再重新操作一遍。