如何“取消还原”已还原的 Git 提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8728093/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:38:23  来源:igfitidea点击:

How do I "un-revert" a reverted Git commit?

gitundorevert

提问by JimmidyJoo

Given a change that has been committed using commit, and then reverted using revert, what is the best way to then undo that revert?

鉴于已使用 提交commit然后使用 还原的更改,revert然后撤消该还原的最佳方法是什么?

Ideally, this should be done with a new commit, so as to not re-write history.

理想情况下,这应该通过新的提交来完成,以免重写历史记录。

采纳答案by Adam Dymitruk

If you haven't pushed that change yet, git reset --hard HEAD^

如果你还没有推动那个改变, git reset --hard HEAD^

Otherwise, reverting the revert is perfectly fine.

否则,还原还原完全没问题。

Another way is to git checkout HEAD^^ -- .and then git add -A && git commit.

另一种方法是git checkout HEAD^^ -- .然后git add -A && git commit

回答by Stephan

git cherry-pick <original commit sha>
Will make a copy of the original commit, essentially re-applying the commit

git cherry-pick <original commit sha>
将复制原始提交,实质上是重新应用提交

Reverting the revert will do the same thing, with a messier commit message:
git revert <commit sha of the revert>

恢复 revert 会做同样的事情,但提交信息更混乱:
git revert <commit sha of the revert>

Either of these ways will allow you to git pushwithout overwriting history, because it creates a new commit after the revert.
When typing the commit sha, you typically only need the first 5 or 6 characters:
git cherry-pick 6bfabc

这两种方式中的任何一种都可以让您git push无需覆盖历史记录,因为它会在还原后创建一个新的提交。
输入 commit sha 时,您通常只需要前 5 或 6 个字符:
git cherry-pick 6bfabc

回答by Nestor Milyaev

A revert commit is just like any other commit in git. Meaning, you can revert it, as in:

恢复提交就像 git 中的任何其他提交一样。意思是,您可以还原它,例如:

git revert 648d7d808bc1bca6dbf72d93bf3da7c65a9bd746

That obviously only makes sense once the changes were pushed, and especially when you can't force push onto the destination branch (which is a good idea for your masterbranch). If the change has not been pushed, just do cherry-pick, revert or simply remove the revert commit as per other posts.

这显然只有在推送更改后才有意义,尤其是当您无法强制推送到目标分支时(这对您的分支来说是个好主意)。如果更改尚未推送,只需按照其他帖子进行挑选、还原或简单地删除还原提交。

In our team, we have a rule to use a reverton Revert commits that were committed in the main branch, primarily to keep the history clean, so that you can see which commit reverts what:

在我们的团队中,我们有一个规则对在主分支中提交的 Revert 提交使用revert,主要是为了保持历史干净,以便您可以看到哪个提交还原了什么:

      7963f4b2a9d   Revert "Revert "OD-9033 parallel reporting configuration"
      "This reverts commit a0e5e86d3b66cf206ae98a9c989f649eeba7965f.
                    ...
     a0e5e86d3b6    Revert "OD-9055 paralel reporting configuration"
     This reverts commit 648d7d808bc1bca6dbf72d93bf3da7c65a9bd746.
                ...
     Merge pull request parallel_reporting_dbs to master* commit 
    '648d7d808bc1bca6dbf72d93bf3da7c65a9bd746'

This way, you can trace the history and figure out the whole story, and even those without the knowledge of the legacy could work it out for themselves. Whereas, if you cherry-pickor rebasestuff, this valuable information is lost (unless you include it in the comment).

这样,你可以追溯历史,弄清楚整个故事,即使不了解遗产的人也可以自己解决。然而,如果你摘樱桃重订的东西,这些有价值的信息丢失(除非你将其包含在注释)。

Obviously, if a commit reverted and re-reverted more than once that becomes quite messy.

显然,如果一个提交不止一次地恢复和重新恢复,那会变得非常混乱。

回答by rafee_que_

Reverting the revert will do the trick

还原还原就行了

For example,

例如,

If abcdefis your commit and ghijklis the commit you have when you reverted the commit abcdef, then run:

如果abcdef是您的提交,并且ghijkl是您恢复提交时的提交abcdef,则运行:

git revert ghijkl

This will revert the revert

这将还原还原

回答by Drew LeSueur

Here's how I did it:
If the branch my_branchnamewas included in a merge that got reverted. And I wanted to unrevert my_branchname:

我是这样做的:
如果分支my_branchname包含在已恢复的合并中。我想恢复原状my_branchname

I first do a git checkout -b my_new_branchnamefrom my_branchname.
Then I do a git reset --soft $COMMIT_HASHwhere $COMMIT_HASHis the commit hash of the commit right beforethe first commit of my_branchname(see git log)
Then I make a new commit git commit -m "Add back reverted changes"
Then I push up the new branch git push origin new_branchname
Then I made a pull request for the new branch.

我先做一个git checkout -b my_new_branchnamefrom my_branchname
然后我做了git reset --soft $COMMIT_HASH这里$COMMIT_HASH是提交的提交正确的哈希的第一次提交的my_branchname(见git log
然后我提出一个新的提交git commit -m "Add back reverted changes"
,然后我推了新的分支git push origin new_branchname
,然后我做了新的分支拉请求。

回答by RredCat

It is looks stupid for me. But I had been in the same situation and I did revert for reverted commits. I did number reverts so I had to do revert for each 'revert commit'.

这对我来说看起来很愚蠢。但是我遇到了同样的情况,并且确实恢复了已恢复的提交。我做了数字还原,所以我必须为每个“还原提交”进行还原。

Now my commits history looks a weird a bit.

现在我的提交历史看起来有点奇怪。

weird history

奇怪的历史

It is a pet project so it is OK. But for real-life project I would give preference to go to last commit before reverted restore all reverted code together in one commit and more reasonable comment.

这是一个宠物项目,所以没关系。但是对于现实生活中的项目,我更喜欢在一次提交和更合理的注释中恢复所有恢复的代码之前的最后一次提交。

回答by Ryan Chou

Or you could git checkout -b <new-branch>and git cherry-pick <commit>the before to the and git rebaseto drop revertcommit. send pull request like before.

或者你可以git checkout -b <new-branch>git cherry-pick <commit>之前的和git rebase放弃revert提交。像以前一样发送拉取请求。

回答by NobodysNightmare

If you don't like the idea of "reverting a revert" (especially when that means losing history information for many commits), you can always head to the git documentation about "Reverting a faulty merge".

如果您不喜欢“还原一个还原”的想法(尤其是当这意味着丢失许多提交的历史信息时),您可以随时前往关于“还原错误合并”的 git 文档。

Given the following starting situation

鉴于以下起始情况

 P---o---o---M---x---x---W---x
  \         /
   A---B---C----------------D---E   <-- fixed-up topic branch

(W is your initial revert of the merge M; D and E are fixes to your initially broken feature branch/commit)

(W 是您对合并 M 的初始还原;D 和 E 是对您最初损坏的功能分支/提交的修复)

You can now simply replay commits A to E, so that none of them "belongs" to the reverted merge:

您现在可以简单地重放提交 A 到 E,以便它们都不“属于”恢复的合并:

$ git checkout E
$ git rebase --no-ff P

The new copy of your branch can now be merged to masteragain:

您的分支的新副本现在可以master再次合并到:

   A'---B'---C'------------D'---E'  <-- recreated topic branch
  /
 P---o---o---M---x---x---W---x
  \         /
   A---B---C----------------D---E

回答by Richa Goyal

To get back the unstaged and staged changes which were reverted after a commit:

要取回在提交后恢复的未暂存和暂存更改:

git reset HEAD@{1}

To recover all unstaged deletions:

要恢复所有未暂存的删除:

git ls-files -d | xargs git checkout --