如何还原多个git commit? [英] How to revert multiple git commits?

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

问题描述

我有一个git存储库,如下所示:

  A-> B-> C-> D-> HEAD 

我希望分支的头部指向A,即我想要B,C,D ,而HEAD消失了,我希望head成为A的代名词。



听起来我可以尝试重新设置基准(不适用,因为我已经按下了或还原。但是,如何还原多个提交?我一次还原一个吗?订单重要吗?

解决方案

扩展我在评论中写的内容



一般规则是,您不应重写(更改)已发布的历史记录,因为有人可能会基于此进行工作。如果您重写(更改)历史记录,则在合并它们的更改以及对其进行更新时会遇到问题。



因此,解决方案是创建一个 new commit ,它还原要删除的更改。您可以使用 git revert 命令。



您遇到以下情况:

 
A<-B<-C< D<-主控<-HEAD

(这里的箭头指的是指针的方向:在提交时是父引用,在分支头(分支ref)中是顶部提交,而在分支中是分支的名称)



您需要创建的内容如下:

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

其中, [[(BCD)^-1]表示可还原提交B,C,D中的更改的提交。数学告诉我们(BCD)^-1 = D ^ -1 C ^ -1 B ^ -1,因此您可以使用t获得所需的情况他的以下命令:

  $ git revert --no-commit D 
$ git revert --no-commit C
$ git restore --no-commit B
$ git commit -m提交消息






另一种解决方案是检出提交A的内容,并提交以下状态:

  $ git checkout -f A-。 
$ git commit -a

然后您将遇到以下情况:

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

提交A'与提交A具有相同的内容,但是是不同的提交(提交消息,父母,提交日期)。 / p>

Jeff Ferland的解决方案,查尔斯·贝利(Charles Bailey)修改基于相同的想法,但是使用 git reset

  $ git reset –hard A 
$ git reset –soft @ {1}#(或ORIG_HEAD),即D
$ git commit


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"


Alternate solution would be to checkout contents of commit A, and commit this state:

$ git checkout -f A -- .
$ 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).

The solution by Jeff Ferland, modified by Charles Bailey builds upon the same idea, but uses git reset:

$ git reset --hard A
$ git reset --soft @{1}  # (or ORIG_HEAD), which is D
$ git commit

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

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