如何在过去的两个任意版本之间注入一个版本? [英] How to inject a version between some two arbitrary versions in the past?

查看:103
本文介绍了如何在过去的两个任意版本之间注入一个版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的本地分支有以下版本历史记录:

Suppose I have the following version history on my local-only branch:

A -- B -- C

如何在A和B之间插入新版本X,以便版本历史记录如下所示:

How do I insert a new version X between A and B, so that the version history looks like this:

A -- X -- B -- C

请注意,在,在我的情况中,它有点不同:由X引入的任何更改都不会传播到B,这应该在A和B之间插入版本X后保持不变。

Note, there is a similar questions on how to insert a commit in the past however, in my case it's a bit different: any changes introduced by X won't propagate to B, which should stay the same after version X is inserted between A and B.

推荐答案

如果您想添加提交,对源代码没有影响,那么是的,你确实需要一个不同于交互式底座的路径。

If you want to add the commit, but make it have no effect on the source, then yes, you do need a different path than an interactive rebase.

没有什么特别为此设计的,但使用它相对容易 git重放ce

There is nothing specifically designed for this, but it's relatively easy to do with git replace:


  1. 检出commit A (as一个分离的HEAD或一个新的分支,但新的分支很快就没用了): git checkout< hash-of-A>

  2. 创建提交 X (但是您希望它出现)。
  3. 运行 git replace - 移植< B的散列> HEAD

  1. Check out commit A (as a detached HEAD, or on a new branch, but the new branch will soon be useless): git checkout <hash-of-A>.
  2. Make commit X (however you want it to appear).
  3. Run git replace --graft <hash-of-B> HEAD.

您现在有这样的实际记录:

You now have this actual history:

  X--B'    <-- refs/replace/<hash-of-B>
 /
A--B--C   <-- whatever-branch-this-is

这些替换对象的特殊之处在于,无论何时Git几乎对任何对象执行任何操作(包括为任何目的使用提交),例如Git都会查看是否存在一个 refs / replace / 名称与对象的哈希ID。由于 B 的替代品,所以Git现在将转向 B'。因此, git log 和其他Git命令的行为就像历史记录一样:

What's special about these "replace" objects is that whenever Git is about to do almost anything with almost any object—including "use a commit for whatever purpose", for instance—Git will look to see if there is a refs/replace/ name with the object's hash ID. Since there is a replacement for B, Git will now look instead to B'. Hence, git log and other Git commands will act as though the history is:

A--X--B'--C

然而,请注意,真实历史仍然存在于这个存储库中。当且仅当 refs / replace / 名称存储在存储库中时,才会使用替换历史记录,当然这是在这个存储库 - 并且替换被启用(它是默认情况下)。但是如果你在其他地方克隆或推送这个资源库,取或推过程通常不会传输任何 refs / replace / 名称;因此该存储库的克隆将切换回旧的历史记录。 (您还可以通过禁用替换来查看原始历史记录,例如使用 git --no-replace-objects log ... )。

Note, however, that the real history is still present in this repository. The substitute history is used if and only if the refs/replace/ name is in the repository—which of course it is, in this repository—and replacement is enabled (which it is by default). But if you clone or push this repository elsewhere, the fetch or push process normally does not transfer any refs/replace/ names; so a clone of this repository will switch back to the old history. (You can also view the original history by disabling replacements, using git --no-replace-objects log ... for instance.)

如果你愿意,你现在可以运行 git filter-branch ,它只是复制提交。默认情况下, git filter-branch 服从替换规则。这意味着当它复制分支上的提交时,它将以 A 开始,然后复制 X ,然后复制 B',然后复制 C 指向 B' A X B'的副本将会与原稿略为一致,所以实际上会重新使用原件;但 C 的副本略有不同:它将使用 B'作为其真正的父级,而不是作为替代,嫁接的父母。所以提交 C 将被复制到新的提交 C',并且分支名称,无论它是什么,都将被创建指向新的副本。

If you like, you can now run git filter-branch, which simply copies commits. By default, git filter-branch obeys the replacement rules. This means that when it copies the commits on the branch, it will start with A, then copy X, then copy B', and then copy C pointing to B'. The copies of A, X, and B' will be bit-for-bit identical to the originals, so will actually re-use the originals; but the copy of C will be slightly different: it will use B' as its real parent, not as a substitute, grafted parent. So commit C will be copied to new commit C' and the branch name, whatever it is, will be made to point to the new copy.

这个过滤的分支有实际的(不只是嫁接)历史,通过 B ' X A ,所以现在如果您克隆已过滤的存储库,在克隆中看到的同样将从 C'开始,并通过 B'返回到 X A

This filtered branch has actual (not just grafted) history that traverses back through B' to X to A, so now if you clone the filtered repository, the history seen in the clone will likewise start from C' and work its way back through B' to X to A.

这篇关于如何在过去的两个任意版本之间注入一个版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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