Git: Unable to create index.lock: File exists
index.lock means a Git process died mid-write or still runs. Check holders, remove the file, then abort or continue the rebase it interrupted.

Introduction & Quick Summary
Every Git command that writes to the index stops with:
fatal: Unable to create '/repo/.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.Quick answer: confirm nothing is running, then delete the one lock file:
pgrep -a git || echo "no git process"
ls -l .git/index.lock
rm -f .git/index.lock
git statusThe lock is not a bug. Git creates <path>.lock next to the file it is about to rewrite, writes the new content there, then renames it over the original — an atomic replace on POSIX filesystems. index.lock exists exactly while a command is rewriting the staging area, and it disappears when that command finishes or crashes. A leftover file therefore means a process died between open and rename.
Causes, most common first: a killed git rebase or git commit, an IDE or file watcher running git status in the background, two CI jobs sharing one workspace, a repository on a network share where atomic rename is not reliable, and a full disk or read-only mount that makes the rename fail after the lock file was created.
Core Mechanics, Signatures & Reproducible Setup
Git's lock protocol is uniform across the repository:
| Lock file | Protects | Created by |
|---|---|---|
.git/index.lock | the staging area | add, commit, reset, merge, rebase, stash, checkout |
.git/HEAD.lock | the current branch pointer | commit, checkout, reset |
.git/refs/heads/<name>.lock | one branch ref | commit, branch -f, update-ref |
.git/config.lock | repository config | git config |
.git/shallow.lock | shallow boundary file | fetch --depth, clone --depth |
.git/gc.pid | a running garbage collection | automatic and manual git gc |
Reproduce the message without killing anything:
mkdir lockdemo && cd lockdemo && git init -q
touch .git/index.lock # simulate a crashed process
git status # still works: status does not write the index
git add . # fatal: Unable to create '.git/index.lock': File exists
rm .git/index.lock
git add . && git statusgit status succeeds while a lock exists because it only reads. That asymmetry is useful: if status works and every write fails, the lock is stale rather than a running process.
Note the third line of the message: "a git process may have crashed". Git cannot distinguish a live holder from a dead one — POSIX file locks are released on process exit, but the file itself remains, so Git refuses to guess whether the content is safe.
Key Comparison Table
Whether deleting is safe depends on what holds the lock:
| Situation | Live holder | Safe to delete | Do instead |
|---|---|---|---|
git rebase killed with Ctrl-C | no | yes | rm .git/index.lock, then git rebase --abort |
| IDE indexing on save | yes, briefly | no | wait, or close the editor |
| two CI jobs, one checkout | yes | no | separate workspaces or a concurrency group |
| disk full during commit | no | yes | free space, then retry |
| network share (NFS/SMB) | unknown | usually | check lsof, prefer a local clone |
git gc running | yes | no | wait for gc.pid to disappear |
The errors you may see instead, and what each points at:
| Message | Meaning | Fix |
|---|---|---|
Unable to create '.git/index.lock': File exists | stale or live lock | check holders, then remove |
Unable to create '.git/index.lock': Permission denied | directory not writable | fix ownership of .git |
Unable to create '.git/index.lock': No space left on device | disk full | free space, check inodes |
fatal: index file smaller than expected | index truncated by a crash | rm .git/index && git reset |
error: could not lock config file .git/config | config.lock present | remove after checking |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Identify the holder before deleting
lsof .git/index.lock 2>/dev/null || echo "not held"
ps -eo pid,etimes,cmd | grep -E '[g]it (commit|rebase|merge|status|gc)'
ls -l --time-style=full-iso .git/index.lockAn hour-old lock with no process is stale. A lock that lsof reports as open is live, and deleting it corrupts the running command's write. On macOS lsof needs the file to exist; on Windows use handle.exe or Process Explorer, since a mapped file cannot be unlinked while open.
2. After removing the lock, finish the interrupted operation
A crashed rebase leaves more than a lock:
ls .git/rebase-merge 2>/dev/null && echo "rebase in progress"
git rebase --abort # or --continue if the conflict is resolved
ls .git/MERGE_HEAD 2>/dev/null && git merge --abortRemoving index.lock alone leaves the repository in a detached, mid-rebase state, and the next git status says "You are currently rebasing" — which looks like a new problem but is the same interrupted one.
3. CI jobs that share one workspace
Two jobs on the same runner checkout fight over the index. Both fixes are one line:
# .github/workflows/ci.yml
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: falseand, for self-hosted runners, actions/checkout with clean: true or a per-job directory. A retry loop around git add is not a fix: it converts a deterministic failure into an intermittent one.
4. Editors, watchers and background git status
VS Code, JetBrains IDEs and several Git GUIs poll git status and run git fetch on a timer. Most of them respect GIT_OPTIONAL_LOCKS=0, which tells Git to skip locks for read-only commands:
GIT_OPTIONAL_LOCKS=0 git statusSet that in the environment of a file watcher rather than globally; a build script that writes should not inherit it.
5. The filesystem, not Git
df -h . # space
df -i . # inodes
mount | grep -E 'nfs|cifs|9p'
touch .git/permcheck && rm .git/permcheck && echo writableNFS and SMB mounts implement rename differently, and Git's lock protocol assumes link() and rename() are atomic. A repository on such a mount produces locks that never disappear or two writers that both believe they hold the lock; the reliable answer is to clone locally and push over the network protocol.
6. Windows, WSL and antivirus
On Windows an index file can stay locked by a virus scanner or by an editor holding a handle, and Git reports File exists even though the file is not visible in Explorer. Closing the editor and pausing real-time scanning on the repository folder resolves it. Accessing one checkout from both Windows and WSL adds CRLF and permission noise on top, so keep separate clones per side.
Common Compilation/Runtime Mistakes & Version Gotchas
Deleting the lock while a process is live. The running command renames its temporary file over the index after you removed the lock, and the staged changes that arrived in between are lost. The index is reconstructible from HEAD (git reset), but only at the cost of the staging work.
Deleting the whole .git directory. It removes the lock and every ref, stash, and reflog entry with it. There is no recovery path from a deleted object database unless a remote still has the commits.
Assuming kill -9 is safe to repeat. Each kill can leave a new lock behind, and a killed git gc can leave a half-pruned pack directory. Let the process finish, or stop the one that spawned it.
Version differences. GIT_OPTIONAL_LOCKS arrived in Git 2.15 and is the standard way to make read commands lock-free. git gc has detached into the background by default since 2.35, so a lock can appear minutes after a fetch finishes. Git 2.40 and later print the holder's concept of the operation (index.lock: another git process...) in more situations, but the remedy is unchanged. The split index feature (core.splitIndex) creates sharedindex.<hash> files and adds a second lock to check when troubleshooting.
Confirming the index is intact after a recovery. Compare the staged tree before and after the fix:
git diff --cached --stat
git ls-files -s | wc -lIf the count drops unexpectedly, git reset rebuilds the index from HEAD and you restage what you need; comparing the staged diff against a saved copy with the text diff tool shows precisely which file entries were lost. When a repository was recovered from a backup or a snapshot, timestamp checks with the Unix timestamp converter settle which copy is newer.
Reference material: git add and git update-index.
Official references
primary documentationThe Git documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit: fatal: not a git repository (or any parent): .git
The command ran outside a working tree or the .git metadata is gone. Confirm the root, restore a deleted .git, and repair worktree pointers.
Git: fatal: Need to specify how to reconcile divergent branches
Git 2.34 and later refuse to guess between merge, rebase and fast-forward. Set pull.rebase or pull.ff once, and know what each strategy does to history.
Git: fatal: unable to auto-detect email address
A commit needs an author identity. Containers and fresh installs have no user.email, so Git guesses from the OS user and refuses to continue.
Git: could not read Username, terminal prompts disabled
A headless git has no terminal to ask for credentials. Supply a token through a helper, a header or SSH instead of relying on a prompt.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.