zoukankan      html  css  js  c++  java
  • 合并两个git仓库并保留提交记录

    case如下:
    1. 有2个git仓库:repo1、repo2;
    2. 想将repo1中的文件移入repo2;
    3. repo1的历史日志要保留;
     
     
    1
    2
    
    # 1、将repo1作为远程仓库,添加到repo2中,设置别名为other
    [jot@myhost repo2]$ git remote add other ../repo1/
    1
    2
    3
    4
    5
    6
    7
    8
    
    # 2、从repo1仓库中抓取数据到本仓库
    [jot@myhost repo2]$ 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
    1
    2
    3
    4
    
    # 3、将repo1仓库抓取的master分支作为新分支checkout到本地,新分支名设定为repo1
    [jot@myhost repo2]$ git checkout -b repo1 other/master
    Branch repo1 set up to track remote branch master from other.
    Switched to a new branch 'repo1'
    1
    2
    3
    
    # 4、切换回repo2的master分支
    [jot@myhost repo2]$ git checkout master
    Switched to branch 'master'
    1
    2
    3
    4
    5
    6
    
    # 5、将repo1合并入master分支
    [jot@myhost repo2]$ git merge repo1
    Merge made by recursive.
     repo1.txt |    1 +
     1 files changed, 1 insertions(+), 0 deletions(-)
     create mode 100644 repo1.txt
    可以看到效果了,日志确实还在:
    已经完成,可以push到服务器了。
    可能遇到的问题:
    1. 在合并时有可能两个分支对同一个文件都做了修改,这时需要解决冲突,对文本文件来说很简单,根据需要对冲突的位置进行处理就可以。对于二进制文件,需要用到如下命令。
    git checkout --theirs YOUR_BINARY_FILES     // 保留需要合并进来的分支的修改
    //git checkout --ours YOUR_BINARY_FILES       // 保留自己的修改
    git add YOUR_BINARY_FILES
    git comm
    总结:
      1. 大致思路是伪造远程的repo1仓库为repo2的一个分支,然后合并进来;
      2. 若是文件有冲突、或要建立子目录,建议在repo1中先解决,再进行如上操作。
  • 相关阅读:
    iptables 详解
    Linux Crontab 定时任务命令详解
    Linux下查看历史操作记录
    Linux shell if 参数
    Linux的五个查找命令:find,locate,whereis,which,type
    linux下IPTABLES配置详解
    Linux命令之while Bash中的While循环
    日志分割脚本
    详解 Too many open files
    微软停止对WindowsNT4.0系统提供无偿的支持
  • 原文地址:https://www.cnblogs.com/suanec/p/8418924.html
Copyright © 2011-2022 走看看