还原多个git提交 [英] Revert multiple git commits

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

问题描述

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

我希望分支的头部指向A,即我想B,C,D ,头部消失,我希望头像是A的同义词。



听起来我可以尝试重新绑定(不适用,因为我推动了之间的变化)或恢复。但是,我如何恢复多个提交?我是否一次回复一个?订单是否重要?

解决方案



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



所以解决方案是创建一个新提交恢复您想要摆脱的更改。你可以使用 git恢复命令。



您有以下情况:

 
A < - B < - C < - D < - 主人 - - 头部

(这里的箭头表示指针的方向:在提交的情况下为父引用,在分支头的情况下为顶部提交(分支引用),以及分支的名称

您需要创建的内容如下:

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

pre $ git revert --no-commit D
$ git revert --no-commit C
$ git revert --no-commit B
$ git commit -m提交信息






其他解决方案是签出提交A的内容,并提交此状态:

  $ 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 @ {1}#(or 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提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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