通过 Git reflog 命令恢复提交
- 查找在分支上不再可见的提交
- git reflog
- 例子
查找在分支上不再可见的提交
Git 不会显示提交,如果它们直接或间接地被引用为指针,如分支或标记。某些 git 命令从您的视图中删除提交,例如: * git reset 可能会从您当前的分支中删除提交,因此这些提交会从您的视图中消失 * 修改提交也会从您的视图中删除提交
例如,假设您有两个提交 A→ B,其中 B 是 A 之后的提交。将分支指针重置为 A,该命令不再包含 B。git log
git reflog命令允许您通过显示 HEAD 指针移动来查找此类提交。
git reflog
Reflog是一种记录HEAD和分支引用的运动的机制。
reflog 命令提供 HEAD 引用的完整更改历史记录。
git reflog
# <output>
# ... snip ...
1f1a73a HEAD@{2}: commit: More chaanges - typo in the commit message
45ca204 HEAD@{3}: commit: These are new changes
cf616d4 HEAD@{4}: commit (initial):
该命令还会列出已删除的提交。git reflog
有多个 reflog:每个分支一个,一个用于 HEAD。对于分支,请使用命令,对于 HEAD,请使用 或 命令。git reflog [branch]git refloggit reflog HEAD
例子
下面的示例演示如何使用 git reflog 将当前本地分支重置为无法再从当前分支访问的提交。
# assume the ID for
# 45ca2045be3aeda054c5418ec3c4ce63b5f269f7
# resets the head for
git reset --hard 45ca2045be3aeda054c5418ec3c4ce63b5f269f7
# see the log
git log
# output shows the history until the 45ca2045be commit
# see all the history including the deletion
git reflog
# <output>
cf616d4 HEAD@{1}: reset: moving to 45ca2045be3aeda054c5418ec3c4ce63b5f269f7
# ... snip ...
1f1a73a HEAD@{2}: commit: More chaanges - typo in the commit message
45ca204 HEAD@{3}: commit: These are new changes
cf616d4 HEAD@{4}: commit (initial): Initial commit
git reset --hard 1f1a73a