假设你的git历史是这样的:
1 2 3 4 5
1-5是单独的修订。你需要删除3,同时仍然保持1、2、4和5。如何做到这一点呢?
当一个要删除的版本之后还有数百个版本时,有没有有效的方法?
假设你的git历史是这样的:
1 2 3 4 5
1-5是单独的修订。你需要删除3,同时仍然保持1、2、4和5。如何做到这一点呢?
当一个要删除的版本之后还有数百个版本时,有没有有效的方法?
当前回答
收音机和kareem的回答对我没有任何帮助(只有消息“当前分支是最新的。”出现)。这可能是因为'^'符号在Windows控制台中不起作用。但是,根据这条注释,将'^'替换为'~1'可以解决这个问题。
git rebase --onto <commit-id>^ <commit-id>
其他回答
要将版本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.
如果你想要做的只是删除在版本3中所做的更改,你可能想要使用git revert。
Git恢复只是创建一个新的版本,其中包含撤销正在恢复的版本中所有更改的更改。
这意味着,您保留了关于不需要的提交和删除这些更改的提交的信息。
如果在同一时间有人从您的存储库中取出数据,这可能会友好得多,因为恢复基本上只是一个标准的提交。
根据这条评论(我检查了这是真的),rado的答案非常接近,但让git处于分离的头部状态。相反,删除HEAD并使用这个从你所在的分支中删除<commit-id>:
git rebase --onto <commit-id>^ <commit-id>
我也遇到了类似的情况。使用下面的命令使用交互式rebase,并在选择时,删除第三次提交。
git rebase -i remote/branch
到目前为止,所有的答案都没有解决后面的问题:
当有数百个修订时,是否有一种有效的方法 在要删除的那个之后?
以下是步骤,但为了参考,让我们假设历史记录如下:
[master] -> [hundreds-of-commits-including-merges] -> [C] -> [R] -> [B]
C: 只在要删除的提交之后提交(干净)
接待员: 要删除的提交
B: 在要删除的提交之前提交(base)
由于“数百个修订”的限制,我假设以下先决条件:
there is some embarrassing commit that you wish never existed there are ZERO subsequent commits that actually depend on that embarassing commit (zero conflicts on revert) you don't care that you will be listed as the 'Committer' of the hundreds of intervening commits ('Author' will be preserved) you have never shared the repository or you actually have enough influence over all the people who have ever cloned history with that commit in it to convince them to use your new history and you don't care about rewriting history
这是一组非常严格的约束条件,但在这种情况下,有一个有趣的答案。
以下是步骤:
git分支B git分支remove-me R Git分支保存 Git rebase -preserve-merges - to base remove-me
如果真的没有冲突,那么这应该继续进行,没有进一步的中断。如果有冲突,你可以解决它们,然后重新开始——继续,或者决定忍受尴尬,重新开始——中止。
现在你应该在master上不再有commit R了。save分支指向您之前的位置,以防您想要调和。
如何安排其他人转移到您的新历史记录取决于您自己。你将需要熟悉隐藏,重置——困难,和樱桃选择。你可以删除基础,删除我,保存分支