git reflog 恢复git reset --hard 回退的内容

人总是会做后悔的事,一开始觉得提交有问题,后面发现还是正确的,问题是你用了git reset --hard HEAD^git reset --hard commit_id回退的本地版本。

问题就出在了--hard

现在也找不到新版本的commit id怎么办?

在Git中,总是有后悔药可以吃的。

1、找到commit_id

hezaizai@ubuntu: ~/code_test $ git reflog
1ab8e23 HEAD@{0}: reset: moving to HEAD^

f8e01df HEAD@{1}: commit: 最新节点

1ab8e23 HEAD@{2}: commit: commit the new f
66759e4 HEAD@{4}: commit: forword
9cb749d HEAD@{6}: commit: twice commit
eb593f8 HEAD@{8}: commit: first commit gitreset”

这里记录了你所有的操作记录

这里f8e01df 就是你回退的HEAD^,也就是那个commit_id。

2、恢复删除记录

推荐: git reset --hard commit_id

好了,世界又恢复了平静。

方法二: git cherry-pick commit_id
git cherry-pick commit_id 会生成新的节点,不是原来的那个,但文件恢复了。可参考我写的git cherry-pick。

一些说明:

1、reflog:Reference logs(参考日志)

2、log和reflog的区别。
git log是显示当前的HEAD和它的祖先的,递归是沿着当前指针的父亲,父亲的父亲,……,这样的原则。
git reflog是HEAD所指向的一个顺序的提交列表:它的undo历史。reflog并不是本地仓库的一部分,单独存储,不包含在pushes,fetches或者clones里面,它只是是本地的。

参考:

https://git-scm.com/docs/git-reflog

你可能感兴趣的:(git,git,github,gitlab)