首頁·終端機命令

git reflog

git

用法

git reflog [show] [<ref>]

記錄 HEAD 和各分支待過的每個位置;被 reset --hard 或刪分支弄丟的提交就在這裡找,項目預設 90 天後過期,而且這份記錄只在你本機。

常用選項

選項含義
show <ref>the positions of one branch instead of HEAD
--date=isoreal timestamps instead of relative ones
-n <n>only the last n entries
HEAD@{n}the commit HEAD pointed at n moves ago
expire --expire=now --allwipe the log, which also gives up the safety net

範例

git reflog

every position HEAD has had, newest first

git reflog --date=iso -n 20

the last twenty moves with real dates

git reset --hard HEAD@{1}

undoes the reset you just regretted

難的地方在於復原有好幾種:把分支的指向挪走,和補一個反向提交,不是一回事。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. git reflog 是做什麼的?

記錄 HEAD 和各分支待過的每個位置;被 reset --hard 或刪分支弄丟的提交就在這裡找,項目預設 90 天後過期,而且這份記錄只在你本機。

Q. 怎麼寫?

git reflog [show] [<ref>] —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 5 個,完整列表在 man git。這條命令屬於git。

相關命令

man 手冊: man git-reflog