简体   繁体   English

如何将多个提交还原为单个提交的一部分

[英]How to revert multiple commits as part of a single commit

This is not a major problem, just something I want to know whether or not is possible. 这不是一个主要问题,只是我想知道是否可能。

Let's say we have two commits, abcd123 and wxyz789 , that occur at non-adjacent, separate places, far back in a repo's history. 假设我们有两个提交, abcd123wxyz789 ,它们发生在非相邻的,分开的地方,远在回购历史中。 Now let's say we want to revert them. 现在让我们说我们想要还原它们。 Doing

git revert abcd123 wxyz789

would result in two separate commits, one reverting abcd123 and the other reverting wxyz789 . 会导致两个单独的提交,一个恢复abcd123 ,另一个恢复wxyz789

This is all fine and well, but what if the mistakes we want to fix in the two commits are logically linked, and for the purposes of self-documentation we'd like to make one single commit containing one single " I broke something so now I'm reverting files x, y and z " comment? 这一切都很好,但是如果我们想要在两个提交中修复的错误在逻辑上是链接的,并且为了自我记录的目的,我们想做一个单独的提交包含一个单词“ 我现在打破了一些东西我正在恢复文件x,y和z “评论? Is there a git command that does this? 有没有这样做的git命令?

(I am of course aware that it is possible to create a commit where I just manually fix all the changes and then push. This is painful for all the obbious reasons.) (我当然知道有可能创建一个提交,我只需手动修复所有更改然后推送。这很麻烦,因为所有的不良原因。)

You can do: 你可以做:

git revert abcd123
git revert --no-commit wxyz789
git commit --amend

... and then write an appropriate commit message describing the combined effect of reverting both commits. ...然后编写一个适当的提交消息,描述恢复两个提交的综合效果。

In case of complicated reverts, which changes each other, the revert --no-commit might be problematic. 如果复杂的恢复相互更改,则revert --no-commit可能会出现问题。

My simple solution was to do real revert, and the squash: 我的简单解决方案是做真正的还原和壁球:

git revert <all commits>
git rebase -i

And then mark all the reverts as squash , except the first one, to create a single commit. 然后将所有恢复标记为squash ,除了第一个,以创建单个提交。

git revert -n <commits>
git commit

The first command will do all the reverts without create any commits and stage the final result (the -n option). 第一个命令将执行所有恢复,而不创建任何提交并暂存最终结果(-n选项)。 After that, the commit command creates a single commit. 之后,commit命令创建一个提交。

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

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