如何将嵌套的git仓库合并到父仓库,保留历史记录? [英] How to merge nested git repo into parent repo, retaining history?

查看:246
本文介绍了如何将嵌套的git仓库合并到父仓库,保留历史记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我知道大多数git专家会立即想到 git rebase ,但我在更一般的意义上使用rebase一词:我有以下结构我的项目:

 
..
.git
工具
lib
src
.git
建立

。和 ./ src 目录显然是 git 存储库,并且每个都有很长的历史和大量的提交。 中的存储库忽略 src 目录(这是它自己的回购)。



我刚刚意识到我只想在中追踪包含源文件的所有内容,因为坦白地说,构建系统是与源代码一起发展,并已变得相当广泛。



我的问题是,我不知道如何让这个存储库保留历史,现在是存放在 src 中。它甚至有可能吗?这就是'rebasing'的意思 - 如果 ./ src / main.c 中的变化被 ./ src / .git ,然后我想保留这些更改并将它们作为新存储库 ./。git 的一部分。


$ b

更新

子树合并是另一回事比我想要的要多,从我所收集的内容来看。总之,它比我需要的要多得多。我只需要旧回购的内容,将开发的所有分支和所有提交,标签等一起看起来好像它们始终是父回购的一部分。在本质上,唯一的变化是文件本身的路径 - 在子回购跟踪 ./ main.c 之前,新的回购将会跟踪。 /src/main.c ,因为我听说过, git 会跟踪内容,而不是文件,然后像上面那样更改文件路径和引用到这些路径,应该是相当微不足道的,正确的?

快速和简单的方法



重命名src中的所有文件,所以它们将以 src / 开头。将src repo添加为远程,获取&合并。删除旧的src回购,现在一切都在 ./



这会让您记录历史记录。

重写历史记录

为了使此合并不可见,您需要使用 git filter-branch --tree-filter 在src存储库中添加 src / 前缀。然后将其添加为 ./ 存储库的远程并获取它(尚未合并)。为了很好地融合历史,您需要重新排序提交。使用 git log --date-order master src / master 以正确的顺序检索这些提交并挑选它们:

  git checkout -b new-master your-first-commit 
git log --format ='%H'--date-order --reverse master src / master | xargs git cherry-pick

这基本上会对您的提交进行合并排序,并将它们排列成线性历史记录。

这不会保留你的合并,所以不要这样做,除非你有平坦的历史。在这种情况下,只能使用filter-branch,然后进行正常的合并。


Now I know most git experts will immediately think of git rebase, but I am using the word "rebase" in the more general sense: I have the following structure of my project:

.
..
.git
tools
lib
src
    .git
build

Both . and ./src directory are evidently git repositories, and have literally a long history and large number of commits each. The repository in . ignores src directory (which is its own repo).

I have just realized that I would instead want to just have one single repo in . tracking everything including the source files, because frankly, the build system is evolving along with the source code, and has become quite extensive.

My problem is that I don't know how to have this repository retain history that is now part of the repository in src. Is it even possible? This is what I meant by 'rebasing' - if changes in ./src/main.c are tracked by ./src/.git across some N commits, then I want to retain these changes and have them be part of the new repository ./.git. Same history, rebased file path.

UPDATE

Subtree merging is something else than what I want, from what I gathered on SO. In short, it does much more than what I need. I merely need the content of the old repo, together will all branches of development and all commits, tags, etc to look as if they were always part of the parent repo. In essense, the only change is paths to files themselves - where before the child repo tracked ./main.c, the new repo will now track ./src/main.c, and since as I have heard, git tracks content, not files, then changing file paths like the above and references to these paths, should be rather trivial, correct?

解决方案

Quick and easy way:

Rename all files in src, so they will start with src/. Add src repo as remote, fetch & merge. Drop old src repo, everything is now in ./.

This will leave you with this action recorded in history.

History rewrite:

To make this merge invisible, you need to use git filter-branch --tree-filter to add src/ prefix in src repository. Then add this as a remote to ./ repository and fetch it (no merge yet). To blend history nicely, you need to reorder commits. Use git log --date-order master src/master to retrieve these commits in correct order and cherry-pick them:

git checkout -b new-master your-first-commit
git log --format='%H' --date-order --reverse master src/master | xargs git cherry-pick

This basically does merge sort on your commits and lines them up to linear history.

This is will not preserve your merges, so dont do it unless you have flat history. In that case use only filter-branch and then do normal merge.

这篇关于如何将嵌套的git仓库合并到父仓库,保留历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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