相同存储库旧版本的加入历史记录 [英] Join history of older version of the same repository

查看:104
本文介绍了相同存储库旧版本的加入历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下历史记录的存储库:

I had a repository with this history:

A---B---C---D

然后,该存储库被拆分"为(基本上,使用git-subtrees创建了另一个存储库,其历史记录从"D"开始).

Then, this repository was "split" (basically, another repository was created with it's history beginning at 'D' with the use of git-subtrees).

现在,我有这个历史记录的另一个仓库:

Now, I have this other repo with this history:

# The same D as the other
D---E---F---G

我该如何加入这两个部分"?将同一项目故事情节整合到一个存储库中?
最终结果必须是:

How can I join these two "parts" of the same project storyline into one repository?
The final result must be:

A---B---C---D---E---F---G

我已经尝试了很多方法,但是所有方法都包括合并,而这并不是我想要的,因为合并不会保留某些更改,例如已删除的文件.
另外,我尝试为该存储库最新版本的所有更改生成补丁,并将其应用在旧版本中,但出现了许多error: <file> already exists in index错误.

I've already tried a number of things but all of them includes merging, and that's not what I intend, because merging doesn't preserve some changes like deleted files.
Also, I tried to generate patches for all changes of this last version of the repository and apply them in the old one, but got lots of error: <file> already exists in index errors.

我发现了这是另一个有关重新育儿的问题一次提交,而这正是解决我的问题的方法,同时结合了git replace --graftgit filter-branch.

I found this other question about re-parenting a commit and that was exactly what solved my problem, a combination of both git replace --graft and git filter-branch.

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

Now my task is complete and I posted the complete, correct answer to the problem below.

推荐答案

更新-实际的完美方法:

准备

# 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),因为现在那里有点混乱.您将有两个彼此独立的历史记录,并且可能会有很多重复的提交.

Now, I strongly suggest that you use a visual tool with this repo (like Gitkraken) since now it's kind of a mess there. You'll have two histories unattached to each other with, possibly, lots of duplicate commits.

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

Now, choose the commits that will be manipulated. Let's call commit with hash A the one in the older history, which will now be a parent of the commit B of the newest history. Now, you can use the script below (or run the commands manually) in order to join the trees and clean the mess left behind (trim the newer history right at the commit B, discarding all parents, since now it has a new parent).
(You must have git-replace and git-filter-repo installed)

#!/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点到旧存储库的所有历史记录重新应用,由于种种原因也无法正常工作,但主要的原因是它没有递归复制其他分支.

Older, not important (that only serves to learn what to NOT do), answer below.

First I tried the approach with git-rebase, that didn't work out for a number of reasons, the biggest one is that it was a bit overkill for something like just changing the parent of a commit to another one, even if it's unrelated to the history.
Then I tried git cherry-pick to reapply all the history from point E..G to the old repository, also didn't work for a number of reasons but the main one was that it didn't copied other branches recursively.

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

$ git replace --graft <commit> <new parent to this commit>
Now, put the HEAD on the tip of the new history (the most recent commit in the main line you want to preserve), then:
$ git filter-branch <new parent to this commit>..HEAD

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

You may loose branches that are not yet merged onto the branch in which the HEAD is, and I couldn't find a way around that for now.

这篇关于相同存储库旧版本的加入历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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