加入同一存储库旧版本的历史记录

Teo*_*oro 5 git git-subtree git-history

我有一个包含此历史记录的存储库:

A---B---C---D
Run Code Online (Sandbox Code Playgroud)

然后,这个存储库被“拆分”(基本上,另一个存储库是使用 git-subtrees 创建的,它的历史从 'D' 开始)。

现在,我有这个历史的另一个回购:

# The same D as the other
D---E---F---G
Run Code Online (Sandbox Code Playgroud)

如何将同一项目故事情节的这两个“部分”合并到一个存储库中?
最终结果必须是:

A---B---C---D---E---F---G
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多东西,但所有这些都包括合并,这不是我想要的,因为合并不会保留一些更改,例如已删除的文件。
此外,我尝试为存储库的最后一个版本的所有更改生成补丁并将它们应用到旧版本中,但遇到了很多error: <file> already exists in index错误。

更新

我发现这个问题,其他关于再养育一个承诺,那就是正是解决我的问题,两者的结合git replace --graftgit filter-branch

更新 2

现在我的任务完成了,我发布了下面问题的完整正确答案。

Teo*_*oro 3

更新 - 实际完美的方法:

准备

# Inside the older repo
$ cd old_repo

# Add the remote to newer repo with updated content
$ git remote add <remote name> <new_repo>

# Fetch the remote
$ git fetch <remote name>

# Track all branches of the remote so you have all of it's history in your older git (be aware of the remote's name in the command)
$ for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##<remote name>/} $b; done

# Delete the remote so you avoid messing up with the newer repo
$ git remote remove <remote name>
Run Code Online (Sandbox Code Playgroud)

现在,我强烈建议您对这个存储库使用可视化工具(例如 Gitkraken),因为现在它有点混乱。您将拥有两个彼此独立的历史记录,并且可能存在大量重复提交。

现在,选择将被操纵的提交。让我们用旧历史记录中的哈希来调用提交,它现在将成为最新历史记录的A提交的父级。B现在,您可以使用下面的脚本(或手动运行命令)来加入树并清理留下的混乱(在提交时修剪较新的历史记录B,丢弃所有父项,因为现在它有一个新的父项)。
(您必须安装 git-replace 和git-filter-repo

#!/bin/sh

# Argument "$1" is commit A, and argument "$2" is commit B of the explanation above

if [ -z "$1" ] || [ -z "$2" ]
then
        echo "You must provide two commit hashes for this script";
        exit 1;
fi

git replace --graft $1 $2
result="$?"

[ "$result" = "0" ] && git filter-repo --force
Run Code Online (Sandbox Code Playgroud)

旧的,不重要(这只是为了学习不该做什么),请在下面回答。

首先,我尝试了使用 的方法git-rebase,由于多种原因,该方法没有成功,最大的一个原因是,对于像将提交的父级更改为另一个提交这样的事情来说有点矫枉过正,即使它与历史无关。
然后我尝试git cherry-pick重新应用从点到旧存储库的所有历史记录E..G,由于多种原因也不起作用,但主要原因是它没有递归复制其他分支。

尝试过的方法

$ git replace --graft <commit> <new parent to this commit>
现在,将 放在HEAD新历史记录的顶部(您要保留的主线中的最新提交),然后:
$ git filter-branch <new parent to this commit>..HEAD

您可能会松散尚未合并到 HEAD 所在分支的分支,但我暂时找不到解决方法。