有2个git仓库:repo1、repo2,想将repo1中的文件移入repo2,repo1的历史日志要保留。
1、将repo1作为远程仓库,添加到repo2中,设置别名为other
git remote add other ../repo1/
2、从repo1仓库中抓取数据到本仓库
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 ../repo1
* [new branch] master -> other/master
3、将repo1仓库抓取的master分支作为新分支checkout到本地,新分支名设定为repo1
git checkout -b repo1 other/master
Branch repo1 set up to track remote branch master from other.
Switched to a new branch 'repo1'
4、切换回repo2的master分支
git checkout master
Switched to branch 'master'
5、将repo1合并入master分支
git merge repo1
上面这个会提示错误,改成下面这样👇(查的资料见文末)
git merge repo1 --allow-unrelated-histories
已经完成,可以push到服务器了。
可能遇到的问题:
1. 在合并时有可能两个分支对同一个文件都做了修改,这时需要解决冲突,对文本文件来说很简单,根据需要对冲突的位置进行处理就可以。对于二进制文件,需要用到如下命令。
git checkout --theirs YOUR_BINARY_FILES // 保留需要合并进来的分支的修改 git checkout --ours YOUR_BINARY_FILES // 保留自己的修改 git add YOUR_BINARY_FILES git commit ...(此处省略)
6、完成之后删除本地repo2中配置的repo1远程仓库地址
git remote rm other
总结
大致思路是伪造远程的repo1仓库为repo2的一个分支,然后合并进来;
若是文件有冲突、或要建立子目录,建议在repo1中先解决,再进行如上操作。
参考:
https://blog.csdn.net/weixin_30659829/article/details/94907377
Git 提示 fatal: refusing to merge unrelated histories
问题原因:两个分支是两个不同的版本,具有不同的提交历史
解决办法:添加 --allow-unrelated-histories 参数,可以允许不相关历史强制合并
参考:https://blog.csdn.net/qq_39400546/article/details/100150320
本站文章除注明转载/出处外,均为原创,若要转载请务必注明出处。转载后请将转载链接通过邮件告知我站,谢谢合作。本站邮箱:admin@only4.work
尊重他人劳动成果,共创和谐网络环境。点击版权声明查看本站相关条款。