一次Git数据恢复

在一次rebase的时候,由于手抖,本来--continue输成了--skip,导致之前的commit丢失了…这才找到了git reflog来恢复commit,特此记录下来。

Git reflog

Reference logs, or “reflogs”, record when the tips of branches and other references were updated in the local repository.

reflog是Git操作的数据恢复的重要命令,它能够记录几乎所有本地仓库的改变,包括我们删除的commit记录。一旦我们执行了hard-reset的操作,rebase --skip就是一种,我们的一部分commit通过git log就会找不到,但是这部分都被reflog记录下来,这样我们只需git reflog就能找到之前的commit,并通过git checkout -b tmp commit-sha创建临时分支,最后merge到当前分支就可以恢复到之前的操作。

1
2
3
4
5
6
7
8
9
# 查看操作记录
git reflog
# 创建临时分支用于merge操作
git chekcout -b tmp commit-sha
git checkout master
git merge tmp