是否可以撤消由以下命令引起的更改?如果是,怎么办?
git reset --hard HEAD~1
是否可以撤消由以下命令引起的更改?如果是,怎么办?
git reset --hard HEAD~1
当前回答
注意:此答案仅在使用IntelliJ等IDE时有效
我最近遇到了一个类似的问题,我既没有进行改变,也没有做出承诺。可以选择当地历史。我能够从IntelliJ的本地历史还原更改(参考)。
希望这对某人有所帮助。
其他回答
答案隐藏在上面的详细响应中,您可以简单地执行以下操作:
$> git reset --hard HEAD@{1}
(参见git-relog show的输出)
这救了我的命:https://medium.com/@CarrieGuss/如何从数字-数字-设置-b830b5e3f60c中恢复
基本上,您需要运行:
for blob in $(git fsck --lost-found | awk ‘$2 == “blob” { print $3 }’); do git cat-file -p $blob > $blob.txt; done
然后手动完成痛苦,将文件重新组织到正确的结构。
注意:千万不要使用git重置,如果你不能100%理解它的工作原理,最好不要使用它。
如果Git尚未垃圾收集,则可以恢复它。
使用fsck获取悬空提交的概述:
$ git fsck --lost-found
dangling commit b72e67a9bb3f1fc1b64528bcce031af4f0d6fcbf
使用rebase恢复悬空提交:
$ git rebase b72e67a9bb3f1fc1b64528bcce031af4f0d6fcbf
IRL案例示例:
$git fsck--找到丢失
Checking object directories: 100% (256/256), done.
Checking objects: 100% (3/3), done.
dangling blob 025cab9725ccc00fbd7202da543f556c146cb119
dangling blob 84e9af799c2f5f08fb50874e5be7fb5cb7aa7c1b
dangling blob 85f4d1a289e094012819d9732f017c7805ee85b4
dangling blob 8f654d1cd425da7389d12c17dd2d88d318496d98
dangling blob 9183b84bbd292dcc238ca546dab896e073432933
dangling blob 1448ee51d0ea16f259371b32a557b60f908d15ee
dangling blob 95372cef6148d980ab1d7539ee6fbb44f5e87e22
dangling blob 9b3bf9fb1ee82c6d6d5ec9149e38fe53d4151fbd
dangling blob 2b21002ca449a9e30dbb87e535fbd4e65bac18f7
dangling blob 2fff2f8e4ea6408ac84a8560477aa00583002e66
dangling blob 333e76340b59a944456b4befd0e007c2e23ab37b
dangling blob b87163c8def315d40721e592f15c2192a33816bb
dangling blob c22aafb90358f6bf22577d1ae077ad89d9eea0a7
dangling blob c6ef78dd64c886e9c9895e2fc4556e69e4fbb133
dangling blob 4a71f9ff8262701171d42559a283c751fea6a201
dangling blob 6b762d368f44ddd441e5b8eae6a7b611335b49a2
dangling blob 724d23914b48443b19eada79c3eb1813c3c67fed
dangling blob 749ffc9a412e7584245af5106e78167b9480a27b
dangling commit f6ce1a403399772d4146d306d5763f3f5715cb5a <- it's this one
$git显示f6ce1a40399772d4146d306d5763f3f5715cb5a
commit f6ce1a403399772d4146d306d5763f3f5715cb5a
Author: Stian Gudmundsen Høiland <stian@Stians-Mac-mini.local>
Date: Wed Aug 15 08:41:30 2012 +0200
*MY COMMIT MESSAGE IS DISPLAYED HERE*
diff --git a/Some.file b/Some.file
new file mode 100644
index 0000000..15baeba
--- /dev/null
+++ b/Some.file
*THE WHOLE COMMIT IS DISPLAYED HERE*
$git rebase f6ce1a403399772d4146d306d5763f3f5715cb5a
First, rewinding head to replay your work on top of it...
Fast-forwarded master to f6ce1a403399772d4146d306d5763f3f5715cb5a.
在回答之前,让我们添加一些背景,解释一下这个HEAD是什么。
首先,什么是头部?
HEAD只是对当前分支上当前提交(最新)的引用。在任何给定时间只能有一个HEAD。(不包括git工作树)
HEAD的内容存储在.git/HEAD中,它包含当前提交的40字节SHA-1。
分离式封头
如果您不在最近一次提交中,这意味着HEAD指向历史上的先前提交,它称为分离的HEAD。
在命令行上,它看起来像-SHA-1而不是分支名称,因为HEAD没有指向当前分支的尖端
关于如何从分离的HEAD恢复的几个选项:
git校验
git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back
这将签出指向所需提交的新分支。此命令将签出到给定的提交。此时,您可以创建一个分支并从此开始工作。
# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>
# create a new branch forked to the given commit
git checkout -b <branch name>
数字刷新
您也可以始终使用reflog。git reflog将显示更新HEAD的任何更改,检查所需的reflog条目将将HEAD设置回该提交。
每次修改HEAD时,reflog中都会有一个新条目
git reflog
git checkout HEAD@{...}
这会让你回到你想要的承诺
git reset HEAD--hard<commit_id>
将你的头“移”回所需的位置。
# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.
注:(自Git 2.7以来)您也可以使用git rebase--也可以不使用autostash。
git还原<sha-1>
“撤消”给定的提交或提交范围。重置命令将“撤消”给定提交中所做的任何更改。将提交带有撤销补丁的新提交,而原始提交也将保留在历史记录中。
# add new commit with the undo of the original one.
# the <sha-1> can be any commit(s) or commit range
git revert <sha-1>
此模式说明了哪个命令执行什么操作。正如您可以看到的那样,重置和签出修改HEAD。