Git 取消恢复

发布于 2024-09-08 22:00:55 字数 200 浏览 0 评论 0原文

在 git 中,假设我提交了 A 和 B

A---[B]

但随后我恢复了

git revert HEAD

所以我现在就在那里:

[A]---B

如何取消我的恢复以便我可以返回到 B?

In git let say I commit A and B

A---[B]

But then I revert with

git revert HEAD

So I am there now:

[A]---B

How do I cancel my revert so that I can go back to B?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

淡墨 2024-09-15 22:00:55

您有两个一般选择:

  • 恢复恢复提交(创建第二个恢复提交,将您带回原始状态)
  • 丢弃恢复提交

git重置--hard HEAD^

仅当您将更改推送到其他任何地方时,第二个选项才适用。事实上,如果您还没有将第一个恢复提交推送到任何地方,您可以简单地使用

git重置--硬

在不创建任何恢复提交的情况下回滚。

You have two general choices:

  • Revert the revert commit (creating a second revert commit that takes you back to the original)
  • Throw away the revert commit with

git reset --hard HEAD^

The second option is only appropriate if you have not pushed your changes anywhere else. In fact, if you haven't pushed your first revert commit anywhere yet, you can simply use

git reset --hard

to roll back without creating any revert commits at all.

恋竹姑娘 2024-09-15 22:00:55

如果您还没有完全完成,即在 gitbash 中您看到类似以下内容:

Username@Host MINGW64 /d/code/your-project (feature|REVERTING)

那么您可以使用 git revert --abort 来中止。

如果您已经完成了......只是不要重置,更改仍然存在。使用 git reset --soft 恢复提交但保留更改。

git reset --soft HEAD^ // discard the last commit, but changes still present in files, staged

另外,如果你想恢复超过 1 次提交:

git reset --soft HEAD~3 // discard last 3 commits, but changes still present in files, staged. Don't know if it works for commits of others.

现在我不再在终端中做事,而是依赖 Intellij IDEA。实际上,在选择一个提交并“将当前分支重置到此处...”后,我从 IDEA 中的这个对话框中了解了软重置与硬重置之间的区别:

在 IDEA 中重置软与硬

非常清晰的解释。

If you haven't done it completely, i.e., in gitbash you see something like:

Username@Host MINGW64 /d/code/your-project (feature|REVERTING)

then you can use git revert --abort to abort.

If you have done it.. just don't hard reset, the changes are still there. Use git reset --soft to revert commit but keeping changes.

git reset --soft HEAD^ // discard the last commit, but changes still present in files, staged

Also, if you want to revert more than 1 commits:

git reset --soft HEAD~3 // discard last 3 commits, but changes still present in files, staged. Don't know if it works for commits of others.

Nowadays I stop doing things in terminal and I rely on Intellij IDEA. Actually I learnt the difference between soft vs. hard reset from this dialog in IDEA after selecting one commit and "Reset current branch to here...":

reset soft vs. hard in IDEA

Very clear explanations.

抱猫软卧 2024-09-15 22:00:55

当您创建一个新的提交(它恢复另一个提​​交)时,您可以将其视为正常提交。

所以基本上你有很多选择,比如

  • git rebase -i(删除恢复提交)
  • git reset --hard(重置为还原之前的提交,您将丢失所有本地更改
  • git reset --soft(与上面相同,但保留本地更改)
  • 从技术上讲,您可以使用 git revert 来恢复您的恢复

As you create a new commit which reverts another commit you can treat it like a normal commit.

So basically you have many choices, such as

  • git rebase -i (remove the revert commit)
  • git reset --hard <commitID> (reset to the commit before the revert, you'll lose all local changes)
  • git reset --soft <commitID> (same as above but keeps local changes)
  • technically you can use git revert <commitId> to revert your revert
决绝 2024-09-15 22:00:55

你能做的就是“挑选”原始提交并再次推送。

What you can do is "cherry-pick" original commit and push again.

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