• git 合并两个不同仓库


    在日常开发过程中,可能会遇到需要将两个不同的仓库合并成到一个仓库的场景。
    这里介绍一下怎么将两个不同的仓库合并到一个仓库中。

    合并两个不同仓库

    思路:添加两个远程仓库,将两个代码作为两个分支,然后手动合并。

    譬如想将 https://github.com/CollegesChat/university-informationhttps://github.com/Reoger/PracticeCode 合并到 PracticeCode 仓库中。

         1.clone PracticeCode 项目

    $ git clone git@github.com:Reoger/PracticeCode.git
    

        2.添加要合并仓库的远程地址

    1. $ git remote add merge_branch git@github.com:CollegesChat/university-information.git
    2. // 为了方便,这里将其命名为 merge_branch

    这里时候,查看远程地址,应该已经有两个地址了

     

      3.从远程仓库下载第二个仓库的代码:

    $ git fetch merge_branch
    

      4.将从 university-information 仓库下载的 master 分支作为要合入到项目 PracticeCode 项目,需要先将其分支 checkout 到一个新分支上

    $ git checkout -b dev merge_branch/master
    

    这里没有冲突。实际项目中可能会有一些冲突,譬如有些文件提示无法删除,subModule 提示问题等等。按照提示解决即可(手动删除或者修改,用 git status 查看冲突位置)。

      5.切换原来的分支,

    $ git checkout master
    

       6.合并 master 分支和 dev 分支

    1. $ git merge --no-ff --allow-unrelated-histories dev
    2. CONFLICT (add/add): Merge conflict in README.md
    3. Auto-merging README.md
    4. CONFLICT (add/add): Merge conflict in .gitignore
    5. Auto-merging .gitignore
    6. Automatic merge failed; fix conflicts and then commit the result.

       7.处理冲突

    1. $ git status
    2. On branch master
    3. Your branch is up to date with 'origin/master'.
    4. You have unmerged paths.
    5. (fix conflicts and run "git commit")
    6. (use "git merge --abort" to abort the merge)
    7. Changes to be committed:
    8. new file: .github/pull_request_template.md
    9. new file: .github/workflows/generate-data.yml
    10. new file: questionnaires/.gitignore
    11. new file: questionnaires/README.md
    12. new file: questionnaires/README_template.md
    13. new file: questionnaires/alias.txt
    14. new file: questionnaires/blacklist.txt
    15. new file: questionnaires/colleges.csv
    16. new file: questionnaires/history.txt
    17. new file: questionnaires/main.py
    18. new file: questionnaires/requirements.txt
    19. new file: questionnaires/results_desensitized.csv
    20. new file: questionnaires/whitelist.txt
    21. Unmerged paths:
    22. (use "git add ..." to mark resolution)
    23. both added: .gitignore
    24. both added: README.md
    25. $ vim .gitignore
    26. $ git add .gitignore
    27. $ vim README.md
    28. $ git add README.md
    29. $ git commit

      8.合并完成~
      看 log,两个仓库的代码完美合并到一个仓库中了

    将 submodule 代码合并到主工程中

    有时候,我们会需要将仓库中 submodule 的代码直接合并到主工程中来。相关操作如下:
    首先从主工程将 submodule 删除:

    1. 1. rm -rf {suModule-path}
    2. // 删除 submodule 目录和文件
    3. 2. vim .gitmodules
    4. // 删除项目目录下 .gitmodules 文件中子模块相关条目
    5. 3. vim .git/config
    6. // 删除配置相中子模块相关条目
    7. 4. rm .git/module/{subModule-path}/*
    8. // 删除模块下的子模块目录,每个子模块对应一个目录,注意只删除对应的子模块目录即可
    9. 5. git rm --cached {subModule-path}
    10. // 如果还有报错的话,把缓存也删除
    1. 在 submodule 的目录结构调整成和之前在主工程相同
    2. 应用上面的合并两个不同仓库的方法将 subModule 的仓库和主工程仓库合并。

  • 相关阅读:
    【毕业设计】Java 基于微信小程序的药店管理系统
    PyG学习 - DATA(torch_geometric.data.Data )
    PHP代码审计18—PHP代码审计小结
    【Java】Java中的零拷贝
    Java集合(二):Map集合与Collections工具类
    面试总结 - 计算机网络
    Jetson系统烧录环境搭建
    原生AJAX
    【Excel函数】文本处理之Text函数
    重塑科普展厅魅力,以用户体验为核心的策略性规划新探索!
  • 原文地址:https://blog.csdn.net/xiaopangcame/article/details/127852783