如何“取消恢复”恢复的 Git 提交?
假设已使用 commit
提交更改,然后使用 revert
还原,那么撤消该还原的最佳方法是什么?
理想情况下,这应该通过新的提交来完成,以免重写历史记录。
Given a change that has been committed using commit
, and then reverted using revert
, what is the best way to then undo that revert?
Ideally, this should be done with a new commit, so as to not re-write history.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
gitcherry-pick<原始提交sha>
将制作原始提交的副本,本质上是重新应用提交
恢复恢复将执行相同的操作,但提交消息更混乱:
git revert
这两种方式都允许您
git Push
而不覆盖历史记录,因为它会在还原后创建一个新的提交。输入提交 sha 时,通常只需要前 5 或 6 个字符:
gitcherry-pick 6bfabc
git cherry-pick <original commit sha>
Will make a copy of the original commit, essentially re-applying the commit
Reverting the revert will do the same thing, with a messier commit message:
git revert <commit sha of the revert>
Either of these ways will allow you to
git push
without 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 reset --hard HEAD^
否则,恢复恢复就完全没问题了。
另一种方法是 git checkout HEAD^^ -- . 然后 git add -A && git 提交。
If you haven't pushed that change yet,
git reset --hard HEAD^
Otherwise, reverting the revert is perfectly fine.
Another way is to
git checkout HEAD^^ -- .
and thengit add -A && git commit
.恢复提交就像 git 中的任何其他提交一样。这意味着,您可以恢复它,如下所示:
这显然只有在推送更改后才有意义,尤其是当您无法强制推送到目标分支时(这对您的 master 来说是一个好主意)分支)。如果更改尚未推送,只需按照其他帖子进行挑选、恢复或简单地删除恢复提交。
在我们的团队中,我们有一条规则,对在主分支中提交的还原提交使用还原,主要是为了保持历史记录干净,以便您可以看到哪个提交还原了什么:
这样,你可以追溯历史,弄清楚整个故事,甚至那些不了解遗产的人也可以自己解决。然而,如果您cherry-pick或rebase内容,这些有价值的信息就会丢失(除非您将其包含在评论中)。
显然,如果一次提交被恢复并重新恢复多次,那就会变得非常混乱。
A revert commit is just like any other commit in git. Meaning, you can revert it, as in:
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 master branch). 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 revert on Revert commits that were committed in the main branch, primarily to keep the history clean, so that you can see which commit reverts what:
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-pick or rebase stuff, 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.
恢复恢复就可以解决问题
例如,
如果
abcdef
是您的提交,ghijkl
是您恢复提交abcdef
时的提交,然后运行:这将恢复恢复
Reverting the revert will do the trick
For example,
If
abcdef
is your commit andghijkl
is the commit you have when you reverted the commitabcdef
, then run:This will revert the revert
如果您错误地进行了恢复:
您只需要运行:
您可以通过运行以下命令获取您的提交 ID:
If you did a revert by mistake:
you will simply need to run:
You can get your commits ID by running:
我是这样做的:
如果分支
my_branchname
包含在已恢复的合并中。我想取消恢复my_branchname
:我首先从
my_branchname
执行git checkout -b my_new_branchname
。然后我执行
git reset --soft $COMMIT_HASH
,其中$COMMIT_HASH
是第一次提交之前提交的提交哈希>my_branchname
(参见git log
)然后我进行一个新的提交
git commit -m "Add back reverted Changes"
然后我推送新分支
git push origin new_branchname
然后我向新分支发出了拉取请求。
Here's how I did it:
If the branch
my_branchname
was included in a merge that got reverted. And I wanted to unrevertmy_branchname
:I first do a
git checkout -b my_new_branchname
frommy_branchname
.Then I do a
git reset --soft $COMMIT_HASH
where$COMMIT_HASH
is the commit hash of the commit right before the first commit ofmy_branchname
(seegit 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.
如果您不喜欢“恢复恢复”的想法(特别是当这意味着丢失许多提交的历史信息时),您可以随时前往有关 "恢复错误的合并"。
给定以下开始情况
(W 是合并 M 的初始恢复;D 和 E 是对最初损坏的功能分支/提交的修复)
您现在可以简单地重播提交 A 到 E,以便它们都不“属于”恢复合并:
分支的新副本现在可以再次合并到
master
: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".
Given the following starting situation
(W is your initial revert of the merge M; D and E are fixes to your initially broken feature branch/commit)
You can now simply replay commits A to E, so that none of them "belongs" to the reverted merge:
The new copy of your branch can now be merged to
master
again:或者,您可以在之前进行
git checkout -b
和gitcherry-pick
以及git rebase
删除恢复
提交。像以前一样发送拉取请求。Or you could
git checkout -b <new-branch>
andgit cherry-pick <commit>
the before to the andgit rebase
to droprevert
commit. send pull request like before.要取回提交后恢复的未暂存和已暂存的更改:
要恢复所有未暂存的删除:
To get back the unstaged and staged changes which were reverted after a commit:
To recover all unstaged deletions:
在意外删除所有文件的最初恐慌之后,我使用以下方法恢复了数据
After the initial panic of accidentally deleting all my files, I used the following to get my data back
我遇到了一个问题,有人将我的分支恢复为 master,但我需要能够再次合并它,但问题是恢复包括我的所有提交。
让我们看一下我们从 M1 创建功能分支的情况,我们在 M3 中合并我们的功能分支并在 RM3 中恢复它
如何使 F2 能够合并到 M5?
I had an issue somebody made a revert to master to my branch, but I was needed to be able to merge it again but the problem is that the revert included all my commit.
Lets look at that case we created our feature branch from M1 we merge our feature branch in M3 and revert on it in RM3
How to make the F2 able to merge to M5?
我看到响应中包含命令 git reset --hard HEAD ,没有任何警告。由于选项
--hard
,您应该小心使用该命令。它会重置您的索引和远程存储库,但最重要的是,它还会重置您的本地存储库,并且所有未推送到远程的提交都将丢失,无论是本地存储库还是索引。切勿使用该标志--hard
,除非您确定还想重置从当前提交到您选择的哈希的所有本地工作。如果您错误地执行了此操作,请运行 git reflog 来检索您的 ~hash,然后运行 git reset --hard ~hash 来恢复您的文件。
I saw responses include the command
git reset --hard HEAD
without any caution. You should be careful with that command because of the option--hard
. It resets your index and your remote repo but mostly, it also resets your local repo and all commits that were not pushed to the remote yet will be lost, both from your local repo and index. Never use that flag--hard
unless you are sure you also want to reset all your local work from the current commit till the hash you chose.If anyway you did it by mistake, run
git reflog
to retrieve your ~hash thengit reset --hard ~hash
to recover your files.就我而言,我需要在恢复后提交更改,然后才能无故障地挑选原始提交。
In my case I needed to commit the changes after revert before I could cherry-pick the original commit without a failure.
另一种方法:从已恢复的提交创建补丁,然后应用该补丁。
您可以通过命令行或 SourceTree 等 ui 工具执行此操作。
如何为特定的项目生成 Git 补丁提交?
Another way: Create a patch from the commit that was reverted and then apply the patch.
You can do this from the command line, or ui tools like SourceTree.
How can I generate a Git patch for a specific commit?