简体   繁体   English

如何撤消还原操作以返回到我在 git 中的先前提交

[英]How to undo a revert action to move back to my previous commit in git

I ran the revert command to undo my previous commit but then realized I had made a mistake.我运行了 revert 命令来撤消我之前的提交,但后来意识到我犯了一个错误。

How can I undo my revert action and restore to my previously committed state?如何撤消我的还原操作并恢复到我之前提交的 state?

Example:例子:

(1) In my project, I have two files A and B and then I committed the changes and push the changes to GitHub. (1) 在我的项目中,我有两个文件 A 和 B,然后我提交了更改并将更改推送到 GitHub。

(2) I added file C and committed changes but didn't push the changes to Github. (2) 我添加了文件 C 并提交了更改,但没有将更改推送到 Github。

(3) I ran the Revert which removed the file C from my project but it was a mistake. (3) 我运行了 Revert,它从我的项目中删除了 C 文件,但这是一个错误。

How can I undo my last action of Revert to bring back the file C, which was committed and then reverted on my local repo but never pushed to Github?如何撤消我的最后一个 Revert 操作以恢复文件 C,该文件已提交然后在我的本地 repo 上恢复但从未推送到 Github?

'git reflog' to list last few HEADS. 'git reflog' 列出最后几个 HEADS。 Choose commit id of the change you wanted.选择您想要的更改的提交 ID。 git reset to that commit id. git 重置为该提交 ID。

Dangit, I did something terribly wrong, please tell me git has a magic time machine??! Dangit,我做错了什么,请告诉我 git 有一个神奇的时间机器??!

 git reflog # you will see a list of every thing you've # done in git, across all branches! # each one has an index HEAD@{index} # find the one before you broke everything git reset HEAD@{index} # magic time machine

You can use this to get back stuff you accidentally deleted, or just to remove some stuff you tried that broke the repo, or to recover after a bad merge, or just to go back to a time when things actually worked.您可以使用它来取回您意外删除的内容,或者只是删除您尝试过的破坏 repo 的一些内容,或者在合并错误后恢复,或者只是将 go 恢复到实际工作的时间。 I use reflog A LOT.我经常使用reflog Mega hat tip to the many many many many many people who suggested adding it!向许多建议添加它的许多人提供超级帽子提示!

Source: https://dangitgit.com/en#magic-time-machine资料来源: https://dangitgit.com/en#magic-time-machine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM