简体   繁体   English

将两个git存储库合并为一个,保留(重命名)主分支

[英]Merge two git repositories into one, keeping (renamed) master branches

Preamble 前言

I have two separate projects that grew into one. 我有两个独立的项目,一起成长为一个。 They both have their own git repository. 他们都有自己的git存储库。

/project/
/project/app/
/project/app/.git/
/project/helper/
/project/helper/.git/

Both git repositories have some unique branches, and both have of course their own master branch. 两个git存储库都有一些独特的分支,当然它们都有自己的master分支。

Question

I would like to merge both projects into one like so: 我想将两个项目合并为一个如此:

/project/.git/

The unique branches aren't relevant and they can be removed. 独特的分支不相关,可以删除它们。 But I would like the master branches to be renamed and kept, eg masterApp and masterHelper , so that I have a new single repository for the whole project with both branches and a new master branch. 但我希望重命名和保存主分支,例如masterAppmasterHelper ,这样我masterHelper以为整个项目创建一个新的单一存储库,其中包含两个分支和一个新的master分支。

Is this possible? 这可能吗? If so, it would involve some renaming trickery because each repository suddenly includes their own parent directory. 如果是这样,它将涉及一些重命名技巧,因为每个存储库突然包含它们自己的父目录。

Details 细节

These repositories are local for now, so we don't have to worry about remote origins or breaking the commit history for other users. 这些存储库目前是本地的,因此我们不必担心远程起源或打破其他用户的提交历史记录。 I do want to keep the commit history for the individual (renamed) master branches though, and (if possible) the files that are staged/tracked in those branches. 我确实希望保留各个(重命名的) master分支的提交历史记录,以及(如果可能)在这些分支中暂存/跟踪的文件。


I have found the following similar question: How do you merge two git repositories? 我发现了以下类似的问题: 如何合并两个git存储库?

That question is about merging one repository into the other repository. 这个问题是关于将一​​个存储库合并另一个存储库中。 That makes this question slightly different, but I think the solution might be close. 这使得这个问题略有不同,但我认为解决方案可能会很接近。 However, I am not experienced enough with git to figure out how that solution would apply to my scenario. 但是,我对git没有足够的经验来弄清楚该解决方案将如何应用于我的场景。

One way to do it would be to make a seperate git repository 一种方法是创建一个单独的git存储库

mkdir combinedProject
cd combinedProject
git init

then add both projects as remotes to the combinedProject 然后将两个项目作为遥控器添加到combinedProject项目中

git remote add -f App /path/to/App
git remote add -f Helper /path/to/Helper

then create separate masterApp and masterHelper branches 然后创建单独的masterAppmasterHelper分支

git branch masterApp --track App/master
git branch masterHelper --track Helper/master

then create a master branch from masterApp and merge masterHelper into it. 然后从masterApp创建一个主分支并将masterHelper合并到其中。

git checkout masterApp
git checkout -b master
git merge masterHelper

Without creating a new repo you could do the same thing in the App repo 如果没有创建新的仓库,您可以在App仓库中执行相同的操作

git checkout -b masterApp

then adding the remote for Helper and creating a masterHelper Branch 然后为Helper添加远程并创建masterHelper分支

git remote add -f Helper /path/to/Helper
git branch masterHelper --track Helper/master

then mergeing masterHelper into master 然后将masterHelper masterHelpermaster

git checkout master
git merge masterHelper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM