git 如何将多个提交还原为单个提交的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10415565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to revert multiple commits as part of a single commit
提问by JimmidyJoo
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. Now let's say we want to revert them. Doing
假设我们有两次提交,abcd123
并且wxyz789
,它们发生在不相邻的独立位置,可以追溯到一个回购的历史。现在假设我们要还原它们。正在做
git revert abcd123 wxyz789
would result in two separate commits, one reverting abcd123
and the other reverting wxyz789
.
将导致两个单独的提交,一个 revertingabcd123
和另一个 reverting 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? Is there a git command that does this?
这一切都很好,但是如果我们想要在两个提交中修复的错误在逻辑上是相关联的,并且为了自我记录的目的,我们希望制作一个包含一个的单一提交“我现在破坏了一些东西我正在恢复文件 x、y 和 z”评论?是否有执行此操作的 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.)
(我当然知道可以创建一个提交,我只需手动修复所有更改然后推送。由于所有明显的原因,这很痛苦。)
回答by Mark Longair
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.
...然后编写适当的提交消息,描述恢复两次提交的综合效果。
回答by ElyashivLavi
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
,除了第一个,以创建单个提交。
回答by lyu
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). After that, the commit command creates a single commit.
第一个命令将在不创建任何提交的情况下执行所有还原并暂存最终结果(-n 选项)。之后,commit 命令会创建一个提交。