如何合并两个 Git 存储库? [英] How do you merge two Git repositories?

查看:36
本文介绍了如何合并两个 Git 存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下场景:

我在自己的 Git 存储库中开发了一个小型实验项目 A.它现在已经成熟,我希望 A 成为更大项目 B 的一部分,该项目拥有自己的大型存储库.我现在想将 A 添加为 B 的子目录.

I have developed a small experimental project A in its own Git repo. It has now matured, and I'd like A to be part of larger project B, which has its own big repository. I'd now like to add A as a subdirectory of B.

如何将 A 合并到 B 中,同时又不会丢失任何一方的历史记录?

How do I merge A into B, without losing history on any side?

推荐答案

可以轻松地将另一个存储库的单个分支放置在保留其历史记录的子目录下.例如:

A single branch of another repository can be easily placed under a subdirectory retaining its history. For example:

git subtree add --prefix=rails git://github.com/rails/rails.git master

这将显示为单个提交,其中 Rails 主分支的所有文件都添加到rails"目录中.然而,提交的标题包含对旧历史树的引用:

This will appear as a single commit where all files of Rails master branch are added into "rails" directory. However the commit's title contains a reference to the old history tree:

添加'rails/' from commit

Add 'rails/' from commit <rev>

其中 是 SHA-1 提交哈希.你仍然可以看到历史,归咎于一些变化.

Where <rev> is a SHA-1 commit hash. You can still see the history, blame some changes.

git log <rev>
git blame <rev> -- README.md

请注意,您无法从此处看到目录前缀,因为这是一个完整的实际旧分支.您应该将此视为通常的文件移动提交:到达时需要额外跳转.

Note that you can't see the directory prefix from here since this is an actual old branch left intact. You should treat this like a usual file move commit: you will need an extra jump when reaching it.

# finishes with all files added at once commit
git log rails/README.md

# then continue from original tree
git log <rev> -- README.md

有更复杂的解决方案,例如手动执行此操作或按照其他答案中的描述重写历史记录.

There are more complex solutions like doing this manually or rewriting the history as described in other answers.

git-subtree 命令是官方 git-contrib 的一部分,一些数据包管理器默认安装它(OS X Homebrew).但是除了git之外,你可能还得自己安装.

The git-subtree command is a part of official git-contrib, some packet managers install it by default (OS X Homebrew). But you might have to install it by yourself in addition to git.

这篇关于如何合并两个 Git 存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆