如何“取消恢复”恢复的 Git 提交?

发布于 2024-12-25 05:41:19 字数 119 浏览 1 评论 0原文

假设已使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

香橙ぽ 2025-01-01 05:41:19

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

绅士风度i 2025-01-01 05:41:19

如果您还没有推送该更改,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 then git add -A && git commit.

长不大的小祸害 2025-01-01 05:41:19

恢复提交就像 git 中的任何其他提交一样。这意味着,您可以恢复它,如下所示:

git revert 648d7d808bc1bca6dbf72d93bf3da7c65a9bd746

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

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

     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'

这样,你可以追溯历史,弄清楚整个故事,甚至那些不了解遗产的人也可以自己解决。然而,如果您cherry-pickrebase内容,这些有价值的信息就会丢失(除非您将其包含在评论中)。

显然,如果一次提交被恢复并重新恢复多次,那就会变得非常混乱。

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

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 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:

     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-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.

时常饿 2025-01-01 05:41:19

恢复恢复就可以解决问题

例如,

如果 abcdef 是您的提交,ghijkl 是您恢复提交 abcdef 时的提交,然后运行:

git revert ghijkl

这将恢复恢复

Reverting the revert will do the trick

For example,

If abcdef is your commit and ghijkl is the commit you have when you reverted the commit abcdef, then run:

git revert ghijkl

This will revert the revert

吻安 2025-01-01 05:41:19

如果您错误地进行了恢复:

git revert <commit-id>

您只需要运行:

git cherry-pick <commit-id>

我必须使用此命令提交我的更改以进行处理。

您可以通过运行以下命令获取您的提交 ID:

git log --pretty=format:"%h - %an, %ar : %s"

If you did a revert by mistake:

git revert <commit-id>

you will simply need to run:

git cherry-pick <commit-id>

I had to commit my changes to processed with this command.

You can get your commits ID by running:

git log --pretty=format:"%h - %an, %ar : %s"
难以启齿的温柔 2025-01-01 05:41:19

我是这样做的:
如果分支 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 unrevert my_branchname :

I first do a git checkout -b my_new_branchname from my_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 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.

不语却知心 2025-01-01 05:41:19

如果您不喜欢“恢复恢复”的想法(特别是当这意味着丢失许多提交的历史信息时),您可以随时前往有关 "恢复错误的合并"

给定以下开始情况

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

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

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

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

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

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

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

 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)

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

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

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

   A'---B'---C'------------D'---E'  <-- recreated topic branch
  /
 P---o---o---M---x---x---W---x
  \         /
   A---B---C----------------D---E
北音执念 2025-01-01 05:41:19

或者,您可以在之前进行 git checkout -bgitcherry-pick 以及 git rebase删除恢复提交。像以前一样发送拉取请求。

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

独孤求败 2025-01-01 05:41:19

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

git reset HEAD@{1}

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

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

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 --
脱离于你 2025-01-01 05:41:19

在意外删除所有文件的最初恐慌之后,我使用以下方法恢复了数据

git reset HEAD@{1}         
git fsck --lost-found      
git show 
git revert <sha that deleted the files>

After the initial panic of accidentally deleting all my files, I used the following to get my data back

git reset HEAD@{1}         
git fsck --lost-found      
git show 
git revert <sha that deleted the files>
傾旎 2025-01-01 05:41:19

我遇到了一个问题,有人将我的分支恢复为 master,但我需要能够再次合并它,但问题是恢复包括我的所有提交。
让我们看一下我们从 M1 创建功能分支的情况,我们在 M3 中合并我们的功能分支并在 RM3 中恢复它

M1 -> M2 -> M3 -> M4- > RM3 -> M5
 \.         /
  F1->F2 -

如何使 F2 能够合并到 M5?

git checkout master
git checkout -b brach-before-revert
git reset --hard M4
git checkout master
git checkout -b new-feature-branch
git reset --hard M1 
git merge --squash brach-before-revert

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

M1 -> M2 -> M3 -> M4- > RM3 -> M5
 \.         /
  F1->F2 -

How to make the F2 able to merge to M5?

git checkout master
git checkout -b brach-before-revert
git reset --hard M4
git checkout master
git checkout -b new-feature-branch
git reset --hard M1 
git merge --squash brach-before-revert
太阳哥哥 2025-01-01 05:41:19

我看到响应中包含命令 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 then git reset --hard ~hash to recover your files.

夜灵血窟げ 2025-01-01 05:41:19

就我而言,我需要在恢复后提交更改,然后才能无故障地挑选原始提交。

git commit -m "Make changes (original commit)."
git revert <original commit hash>
git commit -m "Revert original commit."
git cherry-pick <original commit hash>

In my case I needed to commit the changes after revert before I could cherry-pick the original commit without a failure.

git commit -m "Make changes (original commit)."
git revert <original commit hash>
git commit -m "Revert original commit."
git cherry-pick <original commit hash>
吹泡泡o 2025-01-01 05:41:19

另一种方法:从已恢复的提交创建补丁,然后应用该补丁。

您可以通过命令行或 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文