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

标签 git git-subtree git-history

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

A---B---C---D
然后,这个存储库被“拆分”(基本上,另一个存储库是使用 git-subtrees 创建的,它的历史从 'D' 开始)。
现在,我有这个历史的另一个 repo :
# The same D as the other
D---E---F---G
如何将同一项目故事情节的这两个“部分” merge 到一个存储库中?
最终结果必须是:
A---B---C---D---E---F---G
我已经尝试了很多东西,但所有这些都包括 merge ,这不是我想要的,因为 merge 不会保留一些更改,例如已删除的文件。
此外,我尝试为存储库的最后一个版本的所有更改生成补丁并将它们应用到旧版本中,但得到了很多 error: <file> already exists in index错误。
更新
我找到了 this other question关于重新抚养提交,这正是解决我的问题的方法,两者的结合 git replace --graftgit filter-branch .
更新 2
现在我的任务完成了,我发布了下面问题的完整正确答案。

最佳答案

更新 - 实际完美的方法:
准备

# 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>
现在,我强烈建议你在这个 repo 中使用一个可视化工具(比如 Gitkraken),因为现在那里有点乱。您将有两个相互独立的历史记录,可能会有很多重复的提交。
现在,选择将被操纵的提交。让我们用哈希值调用 commit A旧历史中的那个,现在将成为提交 B 的父级的最新历史。现在,您可以使用下面的脚本(或手动运行命令)来加入树并清理留下的困惑(在提交 B 处修剪较新的历史记录,丢弃所有 parent ,因为现在它有一个新的 parent )。
(你必须安装 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
较旧,不重要(仅用于学习不该做什么),请在下面回答。
首先,我尝试使用 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您可能会松散尚未 merge 到 HEAD 所在分支的分支,我暂时找不到解决方法。

关于git - 加入同一存储库旧版本的历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62463058/

相关文章:

pycharm - 如何让 Pycharm 显示子文件夹的所有 git 提交历史记录?

git - 获取自分支以来所有已编辑文件的列表

git - 如何删除对 git 的提交

Git: stash 远程仓库上的提交消息

git - 管理代码在源代码控制下使用的第三方源代码和二进制文件

git - 如何根据 super 项目分支切换子模块分支

git - Git 中的部分子树上游推送

git - 为什么 filter-branch 上的 --cached 选项会从工作目录中删除文件?

git - 'git cat-file -p <sha1 >': "fatal : Not a valid object name"on random objects from . git/objects