How to fix `rm -f .git/index`

Ever gotten this message?

fatal: Unable to create '/path/to/repo/.git/index.lock': File exists.

If no other git process is currently running, this probably means a
git process crashed in this repository earlier. Make sure no other git
process is running and remove the file manually to continue.

The way to fix this:

rm .git/index.lock

If you instead do this (note the lack of “.lock” at the end):

rm .git/index

then your git repo will behave in very weird ways. For instance, git status won’t work, and other things break in odd ways. Here’s the fix:

git read-tree --reset HEAD

You’ll lose unstaged changes, but you’ll have your repo back.

Goal-Oriented Git book cover

Goal-Oriented Git is our practical, no-nonsense guide to using Git to achieve your goals in everyday situations. If you've ever wanted to improve your Git skills without getting distracted by complex internals, then this book is for you.