将多个git存储库合并为一个,保留分支机构历史记录 [英] Merge multiple git repositories into one, keeping branches history

查看:139
本文介绍了将多个git存储库合并为一个,保留分支机构历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个独立的项目。他们有自己的git仓库。和所有项目的分支同名。

  / project / 
/ project / projA /
/project/projA/.git/
/ project / projB /
/project/projB/.git/
/ project / projC /
/project/projC/.git/
/ project / projD /
/project/projD/.git/

所有的git仓库都有相同的分支名称,当然也有他们自己的主分支。



问题



I想要将所有项目合并为一个,如下所示:

  /Project/.git/ 
/ project / projA /
/ project / projB /
/ project / projC /
/ project / projD /



但是 b
$ b

我想保留所有分支的历史记录。



ps - >我对所有回购拥有相同的分支名称。
例如:用于所有四个项目的分支名称:V6-004

详情



我试过 子模块 子树 ,但两者都不能解决问题。



我也试过这个。

  $ mkdir new_parent_project 
$ cd new_parent_project
$ git init
#现在我们需要创建初始提交。这是至关重要的。
$ touch README.md
$ git add README.md
$ git commit -aminitial commit


$ b

之后

 #合并项目ProjA到子目录ProjA 
$ git remote添加-f ProjA http:// GitUrl
$ git merge -s我们的 - 无提交ProjA / V6-006
$ git read-tree --prefix = ProjA / -u ProjA / V6- 006
$ git commit -m将ProjA合并到子目录中:ProjA

p>

 #合并项目ProjB到子目录ProjB 
$ git remote add -f ProjB http:// GitUrl
$ git merge -s我们的--no-commit ProjB / V6-006
$ git read-tree --prefix = ProjB / -u ProjB / V6-006
$ git commit -m合并ProjB到subdir:ProbB



但是

项目合并,但我只有V6-006的历史。但我没有其他分支机构的历史记录。

解决方案

该解决方案与您尝试的并不遥远。
这只适用于你的不同项目没有共同文件的情况(否则它可能很难解决冲突)

 #创建一个新的repo:
git init all_projects
cd all_project
#让它更容易理解,让我们创建一个新分支
git checkout -b main

#import第一个项目
git remote add projectA http:// projectA
git fetch --all --tags
git checkout masterA projectA / master
git rebase masterA main
#将主分支移动到当前状态
git分支main -f
#现在您的分支主体处于与您的A项目相同的状态

#import第二个项目
git remote add projectB http:// projectB
git fetch --all --tags
git checkout masterB projectB / master
git rebase masterB main
#将主分支移至当前状态
git分支main -f
#现在您的分支main包含projectA和projectB的所有提交

#等..

结果将是一个存储库,其中包含来自项目A的所有提交,然后来自项目B的所有提交,即使项目B在项目A的最后一次提交之前提交了一些提交,但这不应该是一个问题(和历史树会更容易阅读)



编辑:对不起我只是注意到这并不解决您的问题,以获得所有远程分支机构。也许你可以找到一个基于该问题的解决方案,像这样:

  for $ in $(git branch -r | grep projectA | awk -F'/''{print $ 2}'); do 
git checkout $ i projectA / $ i
git rebase $ i main
done

但这会让你的树更复杂,因为所有分支都将从主要提交。


I have four separate projects. They have their own git repository. and the same name of branches for all projects.

 /project/
 /project/projA/
 /project/projA/.git/
 /project/projB/
 /project/projB/.git/
 /project/projC/
 /project/projC/.git/
 /project/projD/
 /project/projD/.git/

All git repositories have the same name of branches, and of course their own master branch.

Question

I would like to merge all projects into one like so:

  /Project/.git/
  /project/projA/
  /project/projB/
  /project/projC/
  /project/projD/

But

i want to keep the history of all branches.

ps -> i have the same name of branches for all repo. for exemple: a branche name used for all four project: V6-004

Details

I tried submodule and subtree but the both doesn't solve the issue.

I tried also this.

  $ mkdir new_parent_project
  $ cd new_parent_project
  $ git init
  # Now we need to create the initial commit. This is essential.
  $ touch README.md
  $ git add README.md
  $ git commit -am "initial commit"

after

  # merge project ProjA into subdirectory ProjA
  $ git remote add -f ProjA http://GitUrl
  $ git merge -s ours --no-commit ProjA/V6-006
  $ git read-tree --prefix=ProjA/ -u ProjA/V6-006
  $ git commit -m "merging ProjA into subdir: ProjA"

after

  # merge project ProjB into subdirectory ProjB 
  $ git remote add -f ProjB http://GitUrl
  $ git merge -s ours --no-commit ProjB/V6-006
  $ git read-tree --prefix=ProjB/ -u ProjB/V6-006
  $ git commit -m "merging ProjB into subdir: ProjB"

but

the projects are merged but i have only the history of V6-006. but i don't have history for the others branches.

解决方案

That solution is not so far from what you tried. This works only if your different projects have no common files (otherwise it can be difficult to solve conflicts)

# create a new repo:
git init all_projects
cd all_project
# to make it more easy to understand, let's create a new branch
git checkout -b main

# import 1st project
git remote add projectA http://projectA
git fetch --all --tags
git checkout masterA projectA/master
git rebase masterA main
# move the main branch to the current state
git branch main -f
# Now your branch main is at the same state as your A project

# import 2st project
git remote add projectB http://projectB
git fetch --all --tags
git checkout masterB projectB/master
git rebase masterB main
# move the main branch to the current state
git branch main -f
# Now your branch main contains all commits from projectA and projectB

# etc..

The result will be a repository with 1st all commits from project A, then all commits from project B, even if the project B has some commits dated before the last commit of project A, but this should not be a problem (and the history tree will be easier to read)

EDIT : Sorry I just notice this not solve your problem to get all remote branches. Maybe you can find a solution based on that question, with something like this:

for i in $(git branch -r |grep projectA|awk -F'/' '{print $2}'); do
  git checkout $i projectA/$i
  git rebase $i main
done

but this would make your tree more complex because all branches will starts from the main commit ..

这篇关于将多个git存储库合并为一个,保留分支机构历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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