将多个 git 存储库合并为一个,保留分支历史记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26502970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:29:30  来源:igfitidea点击:

Merge multiple git repositories into one, keeping branches history

gitgithubmergegit-branchgit-subtree

提问by Anas ZAHOURI

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

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

 /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.

所有 git 存储库都具有相同名称的分支,当然还有它们自己的主分支。

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

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

Details

细节

I tried submoduleand subtreebut 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.

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

采纳答案by Asenar

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)

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

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 }'); 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 maincommit ..

但这会使您的树更复杂,因为所有分支都将从main提交开始..