git reset 之后的数据恢复

0x00

当你执行了 git reset --hard commitID 之后,如果反悔了,怎么办?

0x01

1
2
3
4
5
6
7
8
9
10
Author: ronething <xxxxx@xx.com>
Date: Sun Jun 2 00:03:53 2019 +0800

feat: test2

commit 83e5f71be74bbcc4f7a5df008515eea7b24304d5
Author: ronething <xxxxx@xx.com>
Date: Sun Jun 2 00:03:41 2019 +0800

feat: test

接下来执行 git reset 回滚

1
2
3
4
5
6
7
8
$ git reset --hard 83e5f71be74bbcc4f7a5df008515eea7b24304d5
HEAD 现在位于 83e5f71 feat: test
$ git log
commit 83e5f71be74bbcc4f7a5df008515eea7b24304d5
Author: ronething <xxxxx@xx.com>
Date: Sun Jun 2 00:03:41 2019 +0800

feat: test

此时我又想回去原来的 commit -> feat: test2

1
2
3
4
$ git reflog
83e5f71 (HEAD -> master) HEAD@{0}: reset: moving to 83e5f71be74bbcc4f7a5df008515eea7b24304d5
891d327 HEAD@{1}: commit: feat: test2
83e5f71 (HEAD -> master) HEAD@{2}: commit (initial): feat: test

当你 (在一个仓库下) 工作时,Git 会在你每次修改了 HEAD 时悄悄地将改动记录下来。当你提交或修改分支时,reflog 就会更新。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ git reset --hard 891d327                                 
HEAD 现在位于 891d327 feat: test2
$ git log
commit 891d32752e44dce8209dd9600a665be5b1ca77c5 (HEAD -> master)
Author: ronething <xxxx@xxx.com>
Date: Sun Jun 2 00:03:53 2019 +0800

feat: test2

commit 83e5f71be74bbcc4f7a5df008515eea7b24304d5
Author: ronething <xxxx@xxx.com>
Date: Sun Jun 2 00:03:41 2019 +0800

feat: test

0x02

封面出处:https://wallhaven.cc/w/r2o3k1

参考资料:

https://blog.csdn.net/rifu19950726/article/details/81903769

Git 内部原理 - 维护及数据恢复

Author: ronething
Link: https://blog.ronething.cn/20190601-new.html
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.