假设你的git历史是这样的:

1 2 3 4 5

1-5是单独的修订。你需要删除3,同时仍然保持1、2、4和5。如何做到这一点呢?

当一个要删除的版本之后还有数百个版本时,有没有有效的方法?


当前回答

我也遇到了类似的情况。使用下面的命令使用交互式rebase,并在选择时,删除第三次提交。

git rebase -i remote/branch

其他回答

如前所述,git-rebase(1)是您的朋友。假设提交在你的主分支中,你会这样做:

git rebase --onto master~3 master~2 master

之前:

1---2---3---4---5  master

后:

1---2---4'---5' master

从git-rebase (1):

提交的范围也可以是 删除与rebase。如果我们有 以下情况: E - F - G - H - I - J局部药 然后命令 git rebase - to topicA~5 topicA~3 topicA 会导致移除 提交F和G: E - H”——我——J的局部药 如果F和G在某些方面有缺陷,这是有用的 或者不应该是topicA的一部分。 注意参数to -onto和 参数可以为任意值 commit-ish有效。

下面是一种非交互式删除特定<commit-id>的方法,只知道你想删除的<commit-id>:

git rebase --onto <commit-id>^ <commit-id> HEAD

收音机和kareem的回答对我没有任何帮助(只有消息“当前分支是最新的。”出现)。这可能是因为'^'符号在Windows控制台中不起作用。但是,根据这条注释,将'^'替换为'~1'可以解决这个问题。

git rebase --onto <commit-id>^ <commit-id>

我也遇到了类似的情况。使用下面的命令使用交互式rebase,并在选择时,删除第三次提交。

git rebase -i remote/branch

要将版本3和4合并为一个版本,可以使用git rebase。如果您想删除版本3中的更改,您需要在交互式rebase模式下使用edit命令。如果您希望将这些更改合并到单个修订中,请使用squash。

我曾经成功地使用过这种挤压技术,但以前从未需要删除修订。“拆分提交”下的git-rebase文档希望能给您足够的理解。(或者其他人可能知道)。

从git文档中:

Start it with the oldest commit you want to retain as-is: git rebase -i <after-this-commit> An editor will be fired up with all the commits in your current branch (ignoring merge commits), which come after the given commit. You can reorder the commits in this list to your heart's content, and you can remove them. The list looks more or less like this: pick deadbee The oneline of this commit pick fa1afe1 The oneline of the next commit ... The oneline descriptions are purely for your pleasure; git-rebase will not look at them but at the commit names ("deadbee" and "fa1afe1" in this example), so do not delete or edit the names. By replacing the command "pick" with the command "edit", you can tell git-rebase to stop after applying that commit, so that you can edit the files and/or the commit message, amend the commit, and continue rebasing. If you want to fold two or more commits into one, replace the command "pick" with "squash" for the second and subsequent commit. If the commits had different authors, it will attribute the squashed commit to the author of the first commit.