简体   繁体   English

如何删除/替换 git subtree commit 及其合并提交?

[英]How to remove/replace git subtree commit and its merge commit?

I have a history tree like this:我有一个这样的历史树:

I found that I merged the wrong version of library in (2) commit and want to remove (2) and (3) from history to get that我发现我在 (2) 提交中合并了错误版本的库,并希望从历史记录中删除 (2) 和 (3) 以获取该库

and then create another subtree above (7) but ideally, I want to force replace commit (2) with another version of the library with forcefully rewritten history of my commits (Like if i did rebase -i and mark commits squash with replacement one)然后在(7)上方创建另一个子树,但理想情况下,我想用另一个版本的库强制替换提交(2),并强制重写我的提交历史(就像我确实rebase -i并标记提交壁球替换一个)

Yellow commits are merge commits created by a subtree command黄色提交是由子树命令创建的合并提交
Red commits are squashed subtree commits红色提交是压缩的子树提交

You can make a new commit 2' which contains the other version of the libray.您可以创建一个包含其他版本库的新提交 2'。 Then merge it with 1. This makes a new commit 3'.然后将其与 1 合并。这将生成一个新的提交 3'。 Then you can rebase 5 and 7 onto 3' which makes new commits 5' and 7'.然后你可以将 5 和 7 变基到 3' 上,这使得新提交 5' 和 7'。 This gives you the desired result.这会给你想要的结果。

Of course, the commit IDs of 3', 5' and 7' are different from the IDs of 3, 5 and 7 as you cannot substitute one of the parents of a commit without changing its sha1 checksum.当然,3'、5' 和 7' 的提交 ID 与 3、5 和 7 的 ID 不同,因为您不能在不更改其 sha1 校验和的情况下替换提交的父项之一。

Here is one way to replace the merge introduced in 2 with whatever commit you want :这是用您想要的任何提交替换2引入的合并的一种方法:

  • run git rebase --rebase-merges -i 1 (obviously : replace 1 with the sha for commit 1 ...)运行git rebase --rebase-merges -i 1 (很明显:更换1与SHA对提交1 ...)
  • the scenario will be a bit more intricated than you may have seen for a simple rebase -i :该场景将比您在简单的rebase -i看到的更复杂:
## here is a sample I got, from a history with two merges :
label onto

# Branch br2
reset [new root]
pick df98219 br2
label br2

# Branch br4
reset [new root]
pick 3a30d4d br4
label br4

## the linear history for master is described here :
reset onto
merge -C f47b992 br2 # Commit 3
merge -C e198adf br4 # Commit 5
  • in the last section, replace the line mentionning your commit 3 with break :在最后一节中,用break替换提及您的提交3的行:
...

reset onto
## remove this line to drop the faulty merge commit :
# merge -C f47b992 br2 # Commit 3
## add a 'break' instruction to stop for editing :
break
merge -C e198adf br4 # Commit 5
  • when the rebasing pauses, create the commit you wish to have当变基暂停时,创建您希望拥有的提交
  • then run git rebase --continue然后运行git rebase --continue

If you have any problem during the rebase, you can always run git rebase --abort , and restart from the beginning.如果在 rebase 过程中遇到任何问题,您可以随时运行git rebase --abort ,然后git rebase --abort重新启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM