如何恢复多个 git 提交? [英] How to revert multiple git commits?

查看:28
本文介绍了如何恢复多个 git 提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的 git 存储库:

A <- B <- C <- D <- HEAD

我希望分支的头部指向 A,即我希望 B、C、D 和 HEAD 消失并且我希望 head 与 A 同义.

听起来我可以尝试重新设定基准(不适用,因为我已经推动了两者之间的更改),或者恢复.但是我如何恢复多次提交?我一次还原一个吗?顺序重要吗?

解决方案

扩展我在评论中写的内容

一般规则是您不应该重写(更改)您已发布的历史记录,因为有人可能已经在此基础上进行了工作.如果您重写(更改)历史记录,则会在合并更改和更新更改时遇到问题.

因此,解决方案是创建一个新提交,它还原您想要删除的更改.您可以使用 git revert 命令.

您有以下情况:

<前>A <-- B <-- C <-- D <-- master <-- HEAD

(这里的箭头指的是指针的方向:提交时为父"引用,分支头(branch ref)时为top commit,HEAD时为分支名称参考).

您需要创建的内容如下:

<前>A <-- B <-- C <-- D <-- [(BCD)-1] <-- master <-- HEAD

其中 [(BCD)^-1] 表示恢复提交 B、C、D 中更改的提交.数学告诉我们 (BCD)-1 =D-1 C-1 B-1,所以你可以使用以下命令获得所需的情况:

$ git revert --no-commit D$ git revert --no-commit C$ git revert --no-commit B$ git commit -m "所有这些的提交信息";

适用于除合并提交之外的所有内容.


替代解决方案是 checkout contents,并提交此状态.也适用于合并提交.但是,添加的文件不会被删除.如果您有任何本地更改 git stash 首先:

$ git checkout -f A -- .# 在本地文件的顶部签出该修订$ git commit -a

那么你会遇到以下情况:

<前>A <-- B <-- C <-- D <-- A' <-- master <-- HEAD

提交 A' 与提交 A 的内容相同,但是是不同的提交(提交消息、父项、提交日期).


替代 Jeff Ferland 的解决方案,由 Charles Bailey 修改相同的想法,但使用 git reset.这里稍作修改,这种方式适用于一切:

$ git reset --hard A$ git reset --soft D#(或ORIG_HEAD或@{1} [HEAD的前一个位置]),都是D$ git提交

I have a git repository that looks like this:

A <- B <- C <- D <- HEAD

I want the head of the branch to point to A, i.e. I want B, C, D, and HEAD to disappear and I want head to be synonymous with A.

It sounds like I can either try to rebase (doesn't apply, since I've pushed changes in between), or revert. But how do I revert multiple commits? Do I revert one at a time? Is the order important?

解决方案

Expanding what I wrote in a comment

The general rule is that you should not rewrite (change) history that you have published, because somebody might have based their work on it. If you rewrite (change) history, you would make problems with merging their changes and with updating for them.

So the solution is to create a new commit which reverts changes that you want to get rid of. You can do this using git revert command.

You have the following situation:

A <-- B  <-- C <-- D                                  <-- master <-- HEAD

(arrows here refers to the direction of the pointer: the "parent" reference in the case of commits, the top commit in the case of branch head (branch ref), and the name of branch in the case of HEAD reference).

What you need to create is the following:

A <-- B  <-- C <-- D <-- [(BCD)-1]                   <-- master <-- HEAD

where [(BCD)^-1] means the commit that reverts changes in commits B, C, D. Mathematics tells us that (BCD)-1 = D-1 C-1 B-1, so you can get the required situation using the following commands:

$ git revert --no-commit D
$ git revert --no-commit C
$ git revert --no-commit B
$ git commit -m "the commit message for all of them"

Works for everything except merge commits.


Alternate solution would be to checkout contents of commit A, and commit this state. Also works with merge commits. Added files will not be deleted, however. If you have any local changes git stash them first:

$ git checkout -f A -- . # checkout that revision over the top of local files
$ git commit -a

Then you would have the following situation:

A <-- B  <-- C <-- D <-- A'                       <-- master <-- HEAD

The commit A' has the same contents as commit A, but is a different commit (commit message, parents, commit date).


Alternate solution by Jeff Ferland, modified by Charles Bailey builds upon the same idea, but uses git reset. Here it is slightly modified, this way WORKS FOR EVERYTHING:

$ git reset --hard A
$ git reset --soft D # (or ORIG_HEAD or @{1} [previous location of HEAD]), all of which are D
$ git commit

这篇关于如何恢复多个 git 提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆