我有一个包含这段历史的存储库:
A---B---C---D
然后,这个存储库被“拆分”(基本上,使用 git-subtrees 创建了另一个存储库,其历史记录从“D”开始)。
现在,我有另一个具有这段历史的存储库:
# The same D as the other
D---E---F---G
如何将同一项目故事情节的这两个“部分”加入到一个存储库中?
最终结果必须是:
A---B---C---D---E---F---G
我已经尝试了很多方法,但所有这些都包括合并,但这不是我想要的,因为合并不会保留一些更改,例如已删除的文件。
另外,我尝试为存储库最后一个版本的所有更改生成补丁并将其应用到旧版本中,但得到了很多error: <file> already exists in index
errors.
Update
I found 这另一个问题 https://stackoverflow.com/questions/3810348/setting-git-parent-pointer-to-a-different-parent关于重新调整提交的父级,这正是解决我的问题的方法,两者的结合git replace --graft
and git filter-branch
.
Update 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>
现在,我强烈建议您对这个存储库使用可视化工具(例如 Gitkraken),因为现在它有点混乱。您将拥有两个彼此独立的历史记录,并且可能存在大量重复提交。
现在,选择将被操纵的提交。让我们用哈希调用提交A
较旧历史记录中的一个,现在将成为该提交的父级B
的最新历史。现在,您可以使用下面的脚本(或手动运行命令)来加入树并清理留下的混乱(在提交时修剪较新的历史记录)B
,丢弃所有父母,因为现在它有一个新的父母)。
(你必须有 git-replace 和git-过滤器-repo https://github.com/newren/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
您可能会松散尚未合并到 HEAD 所在分支的分支,但我暂时找不到解决方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)