Debug-Git&GitHub: (1) index file corrupt, (2) cannot lock ref ‘HEAD’(reference broken)

Ryan Lai
4 min readJun 9, 2021

Also on Strikingly:
https://site-4828247-981-6937.mystrikingly.com/blog/debug-git-github-1-index-file-corrupt-2-cannot-lock-ref

前言

2021/04 的 2 個不同時間點,Code 寫到一半時電腦故障,終端機重新
git status,顯示 2 種 Bug 情況:

  1. error: bad signature 0x00000000 fatal: index file corrupt
    ※ fork (GUI, graphical user interface)的 git commit 紀錄全部"暫時"消失
  2. fatal: cannot lock ref ‘HEAD’: unable to resolve reference ‘refs/heads/master’: reference broken
    git commit 紀錄未消失,但無法 commit 新 Code

檢查自己 Git&GitHub 筆記和 Alpha Camp 課程相關內容,發現皆無相關資訊,因此上 Stack Overflow 或 Google,依 (1)關鍵字查詢 或 (2)轉成英文發問形式搜尋(例如:How to fix… 等方式),解決問題並記錄。

產生以上 2 種 Bug 的主因相似:可能因當機(bluescreen)或斷電,造成 Git 寫入錯誤。

一、error: bad signature 0x00000000 fatal: index file corrupt

  1. Situation (問題、主題等)

終端機指令 git status,出現 error: bad signature 0x00000000 fatal: index file corrupt

造成 fork (GUI, graphical user interface) 介面的 git commit 紀錄全部"暫時"消失。

2. Action (各種 trial and error、成功方法、觀念釐清)

終端機指令:(1) rm .git/index、(2) git reset

Stack Overflow

3. Result (成果)

git commit 紀錄回復正常,可重新 commit。

4. Reference

(1) How to fix a corrupt git index

(2) Stackoverflow:How to resolve “Error: bad index — Fatal: index file corrupt” when using Git

二、fatal: cannot lock ref ‘HEAD’: unable to resolve reference ‘refs/heads/master’: reference broken

  1. Situation (問題、主題等)

終端機無法 commit,出現 fatal: cannot lock ref ‘HEAD’: unable to resolve reference ‘refs/heads/master’: reference broken。

git commit 紀錄未消失,但無法 commit 新 Code

2. Action (各種 trial and error、成功方法、觀念釐清)

終端機指令:(1) rm .git/refs/heads/master、(2) git reset

Stack Overflow

3. Result (成果)

git commit 紀錄全部恢復正常,可繼續重新 commit。

4. Reference

(1) Stack Overflow: Git cannot lock ref ‘HEAD’: unable to resolve reference HEAD

--

--

Ryan Lai

Equipped with web development, communication, and business analytical skills.