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

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

问题描述

考虑以下情况:



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



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

解决方案

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

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

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

 从提交中添加'rails /'<转> 

其中< rev> 是一个SHA -1提交哈希。您仍然可以看到历史记录,并指责一些变化。

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

请注意,您从这里看不到目录前缀,因为这是实际的旧分支完好无损。
您应该像通常的文件移动提交一样对待它:当达到它时您需要额外的跳转。

 #完成一次添加的所有文件commit 
git log rails / README.md

#然后从原始树继续
git log< rev> - README.md

有更复杂的解决方案,像手动执行此操作或重写历史记录其他答案。

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


Consider the following scenario:

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.

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

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.

Add 'rails/' from commit <rev>

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.

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天全站免登陆