是否可以撤消由以下命令引起的更改?如果是,怎么办?
git reset --hard HEAD~1
是否可以撤消由以下命令引起的更改?如果是,怎么办?
git reset --hard HEAD~1
当前回答
制作了一个很小的脚本,以便稍微容易地找到要查找的提交:
git fsck--lost found | grep commit | cut-d“”-f 3 | xargs-i git show \{\}| egrp“^commit |日期:”
是的,它可以用锥子或类似的东西做得更漂亮,但它很简单,我只是需要它。可能会为其他人节省30秒。
其他回答
您要做的是指定要还原到的提交的sha1
git reset --hard <sha1 of desired commit>
但不要等太久。。。几周后,git将最终看到提交未被引用,并删除所有blob。
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.
我刚在错误的项目上做了一个硬重置。拯救我生命的是Eclipse的本地历史。据说IntelliJ Idea也有一个,你的编辑也是如此,值得检查:
关于本地历史的Eclipse帮助主题http://wiki.eclipse.org/FAQ_Where_is_the_workspace_local_history_stored%3F
gitreset-hard-您可以使用它还原一个页面,然后可以再次隐藏或从原始页面中提取所有内容
帕特·诺茨是正确的。你可以在几天内收回承诺。git只会在大约一个月后收集垃圾,除非您明确告诉它删除更新的blob。
$ git init
Initialized empty Git repository in .git/
$ echo "testing reset" > file1
$ git add file1
$ git commit -m 'added file1'
Created initial commit 1a75c1d: added file1
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file1
$ echo "added new file" > file2
$ git add file2
$ git commit -m 'added file2'
Created commit f6e5064: added file2
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file2
$ git reset --hard HEAD^
HEAD is now at 1a75c1d... added file1
$ cat file2
cat: file2: No such file or directory
$ git reflog
1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD
f6e5064... HEAD@{1}: commit: added file2
$ git reset --hard f6e5064
HEAD is now at f6e5064... added file2
$ cat file2
added new file
您可以在示例中看到,由于硬重置,file2被删除,但当我通过reflog重置时,它被放回原位。