【Git 救援篇】不小心 git reset --hard?別慌:用 reflog 與底層物件找回丟失的 Commit / [Git Recovery Guide] Accidentally Ran git reset --hard? Rescuing Commits with Git Reflog
在終端機按下 Enter 的瞬間,許多工程師都曾有過心臟漏跳半拍的經歷:原本只想清除暫存,手指卻下意識敲出 git reset --hard HEAD~1 ,剛剛花了一下午寫完並提交的功能代碼,瞬間在編輯器視窗裡消失得無影無蹤。 The moment Enter is pressed, many developers experience that stomach-dropping panic: intending only to clear staged files, fingers muscle-memory typed git reset --hard HEAD~1 , instantly wiping an entire afternoon of newly committed code from editor buffers. 請先深吸一口氣,停下鍵盤上的所有強制寫入指令。Git 的底層設計並不是像磁碟碎紙機一樣立即覆寫資料;事實上,只要那個修改曾經被 git commit 記錄過,你的程式碼絕大多數時候都依然毫髮無傷地躺在硬碟的物件資料庫裡。 Take a deep breath and immediately step away from destructive write commands. Git's underlying architecture is not a shredder that overwrites disk blocks immediately; if your work was committed even once, the files almost certainly remain intact inside the local object store. 圖一|reset --hard 僅後移分支與 HEAD 指標,Commit 物件仍完整保留於物件庫中(原創架構示意圖,非產品截圖) Figure 1 | reset --hard only moves branch and HEAD pointers; the commit object remains intact in the object database (Original architecture ...