同事遇到一個 Git 錯誤,印象裡我有遇過,原本信心滿滿準備找出 KB 文章秒殺它,卻發現上回沒寫筆記。幸好還記得原因跟解法,補上筆記。

錯誤會發生 Cmder 下 Git 指令或使用軟體執行 Git 版控動作時,訊息如下:

fatal: Unable to create 'D:/git-source-control-path/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.

同事跟我的狀況都是在用 VSCode、Visual Studio、Cmder 開啟同一個 Git 版控的資料夾,大多時侯不會有問題,但有極少數情況可能因軟體間 Git 動作衝突冒出錯誤。解決方法很簡單,將 VSCode、Visual Studio 關閉再試一次即可,若還是不行,如錯誤訊息所說,將相關程式關閉,刪除 index.lock 再繼續。

VS 討論區有類似案例: 12,經驗裡發生機率並不高,遇到時關閉 VS/VSCode 再試即可。

index.lock 檔案的用途類似資料庫更新時的鎖定機制,用以避免多個程式同時修改 .git 下的內容造成錯亂,大多只會在 Git 操作過程短暫存在,另一種狀況是 Git 程序當掉殘留,此時關閉相關程式將 index.lock 刪除便可解決問題。延伸閱讀:Understanding and Using Git's index.lock File

Tips of solving "fatal: Unable to create 'git-source-control-path/.git/index.lock': File exists." error.


Comments

# by 11

感謝~你救了我

Post a comment