如何撤消 git reset --hard HEAD~1?
是否可以撤消由以下命令引起的更改? 如果是这样,怎么办?
git reset --hard HEAD~1
Is it possible to undo the changes caused by the following command? If so, how?
git reset --hard HEAD~1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
帕特·诺茨是正确的。 只要提交是在几天之内,您就可以取回提交。 git 仅在大约一个月左右后收集垃圾,除非您明确告诉它删除较新的 blob。
您可以在示例中看到 file2 由于硬重置而被删除,但当我通过引用日志重置时又被放回原位。
Pat Notz is correct. You can get the commit back so long as it's been within a few days. git only garbage collects after about a month or so unless you explicitly tell it to remove newer blobs.
You can see in the example that the file2 was removed as a result of the hard reset, but was put back in place when I reset via the reflog.
您要做的就是指定要恢复到的提交的 sha1。 您可以通过检查 reflog (
git reflog
) 来获取 sha1,然后执行但不要等待太久...几周后 git 最终会看到该提交为未引用并删除所有 blob 。
What you want to do is to specify the sha1 of the commit you want to restore to. You can get the sha1 by examining the reflog (
git reflog
) and then doingBut don't wait too long... after a few weeks git will eventually see that commit as unreferenced and delete all the blobs.
答案隐藏在上面的详细响应中,您可以简单地执行以下操作:(
参见 git reflog show 的输出)
The answer is hidden in the detailed response above, you can simply do:
(See the output of git reflog show)
如果 Git 尚未进行垃圾收集,则可以恢复它。
使用 fsck 获取悬空提交的概述:
使用 rebase 恢复悬空提交:
It is possible to recover it if Git hasn't garbage collected yet.
Get an overview of dangling commits with
fsck
:Recover the dangling commit with rebase:
据我所知,
--hard
将丢弃未提交的更改。 因为这些不被 git 跟踪。 但您可以撤消放弃的提交
。将列出:
其中
4bac331
是废弃的提交
。现在只需将头移至该提交即可:
As far as I know,
--hard
will discard uncommitted changes. Since these aren't tracked by git. But you can undo thediscarded commit
.will list:
where
4bac331
is thediscarded commit
.Now just move the head to that commit:
IRL 案例示例:
$ git fsck --lost-found
$ git show f6ce1a403399772d4146d306d5763f3f5715cb5a
$ git rebase f6ce1a403399772d4146d306d5763f3f5715cb5a
Example of IRL case:
$ git fsck --lost-found
$ git show f6ce1a403399772d4146d306d5763f3f5715cb5a
$ git rebase f6ce1a403399772d4146d306d5763f3f5715cb5a
git reflog
gitcherry-pick
git reflog
git cherry-pick <the sha>
如果您使用 JetBrains IDE(任何基于 IntelliJ 的东西),您甚至可以通过其“本地历史记录”功能恢复未提交的更改。
右键单击文件树中的顶级目录,在上下文菜单中找到“本地历史记录”,然后选择“显示历史记录”。 这将打开一个视图,可以在其中找到您最近的编辑,找到要返回的修订版本后,右键单击它并单击“恢复”。
If you are using a JetBrains IDE (anything IntelliJ based), you can recover even your uncommited changes via their "Local History" feature.
Right-click on your top-level directory in your file tree, find "Local History" in the context menu, and choose "Show History". This will open up a view where your recent edits can be found, and once you have found the revision you want to go back to, right click on it and click "Revert".
如果您尚未对存储库进行垃圾收集(例如使用 git repack -d 或 git gc,但请注意垃圾收集也可以自动发生),那么您的提交仍然是那里——只是不再可以通过 HEAD 到达。
您可以尝试通过查看 git fsck --lost-found 的输出来查找您的提交。
较新版本的 Git 有一种称为“reflog”的东西,它是对 ref 所做的所有更改的日志(而不是对存储库内容所做的更改)。 因此,例如,每次您切换 HEAD 时(即每次您执行 git checkout 来切换分支时)都会被记录下来。 当然,您的 git reset 也操作了 HEAD,因此它也被记录下来。 您可以以与访问存储库的旧状态类似的方式访问引用的旧状态,即使用
@
符号而不是~
,例如git 重置 HEAD@{1}
。我花了一段时间才理解 HEAD@{1} 和 HEAD~1 之间的区别,所以这里有一点解释:
所以,
HEAD~1
意味着“在提交之前转到提交” HEAD 当前指向的位置”,而HEAD@{1}
表示“转到 HEAD 在指向当前指向的位置之前所指向的提交”。这将让您轻松找到丢失的提交并恢复它。
If you have not yet garbage collected your repository (e.g. using
git repack -d
orgit gc
, but note that garbage collection can also happen automatically), then your commit is still there – it's just no longer reachable through the HEAD.You can try to find your commit by looking through the output of
git fsck --lost-found
.Newer versions of Git have something called the "reflog", which is a log of all changes that are made to the refs (as opposed to changes that are made to the repository contents). So, for example, every time you switch your HEAD (i.e. every time you do a
git checkout
to switch branches) that will be logged. And, of course, yourgit reset
also manipulated the HEAD, so it was also logged. You can access older states of your refs in a similar way that you can access older states of your repository, by using an@
sign instead of a~
, likegit reset HEAD@{1}
.It took me a while to understand what the difference is between HEAD@{1} and HEAD~1, so here is a little explanation:
So,
HEAD~1
means "go to the commit before the commit that HEAD currently points at", whileHEAD@{1}
means "go to the commit that HEAD pointed at before it pointed at where it currently points at".That will easily allow you to find your lost commit and recover it.
在回答之前,让我们添加一些背景知识,解释一下这个
HEAD
是什么。首先什么是 HEAD?
HEAD
只是对当前提交(最新)的引用分支机构。在任何给定时间只能有一个
HEAD
。 (不包括git worktree
)HEAD
的内容存储在.git/HEAD
中,它包含当前提交的 40 字节 SHA-1 。分离的 HEAD
如果您不是最新的提交 - 意味着
HEAD
指向历史记录中的先前提交它被称为分离的HEAD
。在命令行上,它看起来像这样 - SHA-1 而不是分支名称,因为
HEAD
没有指向当前分支的尖端< /a>
关于如何从分离的 HEAD 中恢复的一些选项:
git checkout
这将检出指向所需提交的新分支。
此命令将签出给定的提交。
此时您可以创建一个分支并从此时开始工作。
git reflog
您始终可以使用
reflog< /code> 也是如此。
git reflog
将显示更新HEAD
的任何更改,并检查所需的 reflog 条目会将HEAD
设置回此提交。每次修改 HEAD 时,
reflog
中都会有一个新条目这将使您回到所需的提交
git reset HEAD --hard
< /strong>将你的头“移”回所需的提交。
您还可以使用 git rebase --no-autostash 。
git revert
“撤消”给定的提交或提交范围。
重置命令将“撤消”给定提交中所做的任何更改。
带有撤消补丁的新提交将被提交,而原始提交也将保留在历史记录中。
此架构说明了哪个命令执行什么操作。
正如你所看到的,
reset && checkout
修改HEAD
。Before answering lets add some background, explaining what is this
HEAD
.First of all what is HEAD?
HEAD
is simply a reference to the current commit (latest) on the current branch.There can only be a single
HEAD
at any given time. (excludinggit worktree
)The content of
HEAD
is stored inside.git/HEAD
and it contains the 40 bytes SHA-1 of the current commit.detached HEAD
If you are not on the latest commit - meaning that
HEAD
is pointing to a prior commit in history its calleddetached HEAD
.On the command line it will look like this- SHA-1 instead of the branch name since the
HEAD
is not pointing to the the tip of the current branchA few options on how to recover from a detached HEAD:
git checkout
This will checkout new branch pointing to the desired commit.
This command will checkout to a given commit.
At this point you can create a branch and start to work from this point on.
git reflog
You can always use the
reflog
as well.git reflog
will display any change which updated theHEAD
and checking out the desired reflog entry will set theHEAD
back to this commit.Every time the HEAD is modified there will be a new entry in the
reflog
This will get you back to your desired commit
git reset HEAD --hard <commit_id>
"Move" your head back to the desired commit.
you can also use the
git rebase --no-autostash
as well.git revert <sha-1>
"Undo" the given commit or commit range.
The reset command will "undo" any changes made in the given commit.
A new commit with the undo patch will be commited while the original commit will remain in the history as well.
This schema illustrate which command does what.
As you can see there
reset && checkout
modify theHEAD
.在大多数情况下,是的。
根据运行命令时存储库所处的状态,git reset --hard 的效果范围可以从微不足道到撤消,甚至基本上不可能。
下面我列出了一系列不同的可能情况,以及如何从中恢复。
我所有的更改都已提交,但现在提交都消失了!
当您运行带参数的 git reset 时,通常会发生这种情况,如 git reset --hard HEAD~ 中所示。 别担心,这很容易恢复!
如果您刚刚运行了 git reset 并且此后没有执行任何其他操作,您可以使用这句话返回到原来的位置:
这将重置您当前的分支,无论它在上次之前处于什么状态它已被修改(在您的情况下,对分支的最新修改将是您尝试撤消的硬重置)。
但是,如果您在重置后对分支进行了其他修改,则上面的一行代码将不起作用。 相反,您应该运行
git reflog
查看最近对分支所做的所有更改(包括重置)的列表。 该列表将如下所示:在此列表中查找您想要“撤消”的操作。 在上面的示例中,它将是第一行,即“重置:移动到 HEAD~”。 然后复制该操作之前(下方)的提交表示。 在我们的例子中,这将是
master@{1}
(或3ae5027
,它们都代表相同的提交),然后运行 git reset --hard
commit>
将当前分支重置回该提交。我使用 git add 暂存了更改,但从未提交。 现在我的改变消失了!
恢复起来有点困难。 git 确实拥有您添加的文件的副本,但由于这些副本从未与任何特定提交绑定,因此您无法立即恢复所有更改。 相反,您必须在 git 数据库中找到各个文件并手动恢复它们。 您可以使用
git fsck
来完成此操作。有关详细信息,请参阅对暂存区域中未提交的文件撤消 git reset --hard。
我对工作目录中的文件进行了更改,但从未使用 git add 暂存,也从未提交。 现在我的改变消失了!
呃哦。 我不想告诉你这个,但你可能不走运。 git 不会存储您未添加或提交的更改,并且根据 文档
git重置
:您可能可以通过以下方式恢复更改:某种磁盘恢复实用程序或专业的数据恢复服务,但在这一点上,这可能比它的价值更麻烦。
In most cases, yes.
Depending on the state your repository was in when you ran the command, the effects of
git reset --hard
can range from trivial to undo, to basically impossible.Below I have listed a range of different possible scenarios, and how you might recover from them.
All my changes were committed, but now the commits are gone!
This situation usually occurs when you run
git reset
with an argument, as ingit reset --hard HEAD~
. Don't worry, this is easy to recover from!If you just ran
git reset
and haven't done anything else since, you can get back to where you were with this one-liner:This resets your current branch whatever state it was in before the last time it was modified (in your case, the most recent modification to the branch would be the hard reset you are trying to undo).
If, however, you have made other modifications to your branch since the reset, the one-liner above won't work. Instead, you should run
git reflog
<branchname>
to see a list of all recent changes made to your branch (including resets). That list will look something like this:Find the operation in this list that you want to "undo". In the example above, it would be the first line, the one that says "reset: moving to HEAD~". Then copy the representation of the commit before (below) that operation. In our case, that would be
master@{1}
(or3ae5027
, they both represent the same commit), and rungit reset --hard <commit>
to reset your current branch back to that commit.I staged my changes with
git add
, but never committed. Now my changes are gone!This is a bit trickier to recover from. git does have copies of the files you added, but since these copies were never tied to any particular commit you can't restore the changes all at once. Instead, you have to locate the individual files in git's database and restore them manually. You can do this using
git fsck
.For details on this, see Undo git reset --hard with uncommitted files in the staging area.
I had changes to files in my working directory that I never staged with
git add
, and never committed. Now my changes are gone!Uh oh. I hate to tell you this, but you're probably out of luck. git doesn't store changes that you don't add or commit to it, and according to the documentation for
git reset
:It's possible that you might be able to recover your changes with some sort of disk recovery utility or a professional data recovery service, but at this point that's probably more trouble than it's worth.
如果你真的很幸运,就像我一样,你可以返回文本编辑器并点击“撤消”。
我知道这并不是一个真正正确的答案,但它节省了我半天的工作,所以希望它对其他人也能起到同样的作用!
If you're really lucky, like I was, you can go back into your text editor and hit 'undo'.
I know that's not really a proper answer, but it saved me half a day's work so hopefully it'll do the same for someone else!
我知道这是一个旧线程......但由于许多人正在寻找撤消 Git 中的内容的方法,我仍然认为继续在这里提供提示可能是个好主意。
当您执行“git add”或在 git gui 中将任何内容从左上角移动到左下角时,文件的内容将存储在 blob 中,并且可以从该 blob 中恢复文件内容。
因此,即使文件未提交但必须已添加,也可以恢复该文件。
现在,blob 已创建,但它由索引引用,因此在我们重置之前不会使用 git fsck 列出它。 所以我们重置...
你会得到一个悬空的 blob ce013625030ba8dba906f756967f9e9ca394464a
会给你文件内容“hello”回来
为了查找未引用的提交,我在某处找到了一个建议这一点的提示。
我把它作为 git gui 中的一个工具,非常方便。
I know this is an old thread... but as many people are searching for ways to undo stuff in Git, I still think it may be a good idea to continue giving tips here.
When you do a "git add" or move anything from the top left to the bottom left in git gui the content of the file is stored in a blob and the file content is possible to recover from that blob.
So it is possible to recover a file even if it was not committed but it has to have been added.
Now the blob is created but it is referenced by the index so it will no be listed with git fsck until we reset. So we reset...
you will get a dangling blob ce013625030ba8dba906f756967f9e9ca394464a
will give you the file content "hello" back
To find unreferenced commits I found a tip somewhere suggesting this.
I have it as a tool in git gui and it is very handy.
我刚刚对错误的项目进行了硬重置。 拯救我生命的是 Eclipse 的本地历史。 据说 IntelliJ Idea 也有一个,您的编辑器也可以,值得检查一下:
I've just did a hard reset on wrong project. What saved my life was Eclipse's local history. IntelliJ Idea is said to have one, too, and so may your editor, it's worth checking:
制作了一个小脚本,以便更容易地找到正在寻找的提交:
git fsck --lost-found | grep 提交 | 切 -d ' ' -f 3 | xargs -i git show \{\} | xargs -i git show \{\} | xargs -i git show \{\} | egrep '^commit |Date:'
是的,使用 awk 或类似的东西可以使它变得相当漂亮,但它很简单,我只是需要它。 可能会为其他人节省 30 秒。
Made a tiny script to make it slightly easier to find the commit one is looking for:
git fsck --lost-found | grep commit | cut -d ' ' -f 3 | xargs -i git show \{\} | egrep '^commit |Date:'
Yes, it can be made considerably prettier with awk or something like it, but it's simple and I just needed it. Might save someone else 30 seconds.
注意:这个答案仅在您使用像 IntelliJ 这样的 IDE 时才有效。
我最近遇到了类似的问题,我既没有暂存更改也没有提交更改。 有当地历史的选项。 我能够恢复 IntelliJ 本地历史记录中的更改 (ref) 。
希望它能帮助某人。
Note: this answer is only valid if you use IDEs like IntelliJ
I recently faced a similar issue where I neither staged my changes nor committed them. There is an option of local history. I was able to revert changes from the local history of IntelliJ (ref).
Hope it helps someone.
这救了我的命:
https://medium.com/@CarrieGuss/how-to-recover-from-a-git-hard-reset-b830b5e3f60c
基本上你需要运行:
然后手动经历痛苦来重新 -以正确的结构组织您的文件。
要点:如果您没有完全 100% 理解它的工作原理,切勿使用 git reset --hard ,最好不要使用它。
This has saved my life:
https://medium.com/@CarrieGuss/how-to-recover-from-a-git-hard-reset-b830b5e3f60c
Basically you need to run:
Then manually going through the pain to re-organise your files to the correct structure.
Takeaway: Never use
git reset --hard
if you dont completely 100% understand how it works, best not to use it.git reflog
并返回到最后一个 HEAD6a56624 (HEAD -> master) HEAD@{0}: 重置: 移动到 HEAD~3
1a9bf73 HEAD@{1}:提交:在模型生成二进制文件中添加更改
git reflog
and back to the last HEAD6a56624 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~3
1a9bf73 HEAD@{1}: commit: add changes in model generate binary
我的问题几乎相似。 在输入 git reset --hard 之前我有未提交的文件。
值得庆幸的是。 我设法跳过所有这些资源。 当我注意到我可以撤消(
ctrl-z
for windows/linuxcmd-shift-z
for mac)。My problem is almost similar. I have uncommitted files before I enter
git reset --hard
.Thankfully. I managed to skip all these resources. After I noticed that I can just undo (
ctrl-z
for windows/linuxcmd-shift-z
for mac). ???? I just want to add this to all of the answers above.Note. Its not possible to undo unopened files.
(Or, This is Why We Probably Shouldn’t but Totally Can Have Nice Things)
git reset --hard - 您可以用来恢复一页,之后您可以再次从原点隐藏或提取所有内容
git reset --hard - you can use to revert one page and after that you can stash or pull everything from origin again
小型可执行文件,将所有内容放入 output.txt 文件中。 如果您不小心删除了所有尚未提交的文件(也在
git init
之后),则很有用:Small executable which put's all contents into a output.txt file. Useful if you accidentally deleted all files which weren't committed yet (also after a
git init
):