掃描當前 git repo 中的孤兒 (unreachable / dangling) commits,協助救回:
- 已
git stash drop但還沒被 GC 的 stash git reset --hard之後找不到的 commit- 已刪除 branch 但 tip 還留在物件庫的提交
- 任何因 rebase / amend 而脫鏈、尚未被
git gc清掉的提交
底層使用 git fsck --unreachable,預設不含 reflog 可達物件;加上 -full 會把 reflog 也一起納入掃描。
本專案尚未發佈到公開 Go module registry,請先 clone 後在本地安裝:
git clone <this-repo-url>
cd find-lost-commits
# 方式 A:安裝到 $(go env GOBIN)(沒設定就是 $(go env GOPATH)/bin),可全域呼叫
go install .
# 方式 B:只編譯出當前資料夾的 binary
go build -o find-lost-commits .確認 $(go env GOPATH)/bin 已加入 PATH,之後就能直接 find-lost-commits 呼叫。
需求:Go 1.24+、系統有 git 可在 PATH 中找到。
若之後要支援
go install <path>@latest,需要把go.mod的 module path 改成可解析的遠端位置(例如github.com/TomoakiChen/find-lost-commits)並推到該 repo。
find-lost-commits [-grep <text>] [-since YYYY-MM-DD] [-full]
一律在「當前 cwd 所在的 git repo」運作,不需傳入路徑參數。輸出依日期排序,最新的在最上面。
| Flag | 說明 |
|---|---|
-grep <text> |
用 commit subject 過濾(不分大小寫的子字串比對) |
-since YYYY-MM-DD |
只列出該日期(含)之後的 commit |
-full |
連同 reflog 可達物件一起掃,搜尋範圍更廣 |
-h, --help |
顯示說明 |
# 列出所有 unreachable commits(最新在上)
find-lost-commits
# subject 含 "AkiCSUtil" 的(不分大小寫)
find-lost-commits -grep AkiCSUtil
# 2026-05-01 之後的
find-lost-commits -since 2026-05-01
# 更廣搜尋:把 reflog 可達物件也一起列出
find-lost-commits -fullFound 3 lost commit(s):
a1b2c3d4 2026-05-19 23:11:02 +0800 WIP: refactor AkiCSUtil chunking
9f8e7d6c 2026-05-18 17:42:55 +0800 fix: serializer drops null fields
5544aa11 2026-05-12 09:08:31 +0800 experiment: parallel scan
Inspect: git show <sha> --stat
Apply (stash-like): git stash apply <sha>
Lock as branch: git branch backup/<name> <sha>
找到目標 SHA 後,可依情境選一種方式接回:
# 1. 先看內容
git show <sha> --stat
git show <sha>
# 2a. 如果原本是 stash:像 stash 一樣套用回工作區
git stash apply <sha>
# 2b. 如果原本是 branch tip:用新 branch 釘住,避免之後被 GC
git branch backup/recovered <sha>
# 2c. 只想拿其中部分檔案
git checkout <sha> -- path/to/file注意:
git gc會清掉真正不可達的物件。發現遺失後盡快搶救,並避免在此期間執行git gc --prune=now或git reflog expire。
- 執行
git fsck --unreachable(預設加上--no-reflogs,可用-full取消)。 - 過濾出
unreachable commit <sha>行。 - 對每個 SHA 跑
git show --no-patch --format=%ai|%s取得 author date 與 subject。 - 套用
-grep/-since過濾,依日期由新到舊排序後輸出。