Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readGit
#git#index-lock#rebase#ci
Bold white headline reading STALE LOCK with a Git index crate blocked by an amber padlock on the right.

Introduction & Quick Summary

Every Git command that writes to the index stops with:

code
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:

bash
pgrep -a git || echo "no git process" ls -l .git/index.lock rm -f .git/index.lock git status

The 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 fileProtectsCreated by
.git/index.lockthe staging areaadd, commit, reset, merge, rebase, stash, checkout
.git/HEAD.lockthe current branch pointercommit, checkout, reset
.git/refs/heads/<name>.lockone branch refcommit, branch -f, update-ref
.git/config.lockrepository configgit config
.git/shallow.lockshallow boundary filefetch --depth, clone --depth
.git/gc.pida running garbage collectionautomatic and manual git gc

Reproduce the message without killing anything:

bash
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 status

git 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:

SituationLive holderSafe to deleteDo instead
git rebase killed with Ctrl-Cnoyesrm .git/index.lock, then git rebase --abort
IDE indexing on saveyes, brieflynowait, or close the editor
two CI jobs, one checkoutyesnoseparate workspaces or a concurrency group
disk full during commitnoyesfree space, then retry
network share (NFS/SMB)unknownusuallycheck lsof, prefer a local clone
git gc runningyesnowait for gc.pid to disappear

The errors you may see instead, and what each points at:

MessageMeaningFix
Unable to create '.git/index.lock': File existsstale or live lockcheck holders, then remove
Unable to create '.git/index.lock': Permission denieddirectory not writablefix ownership of .git
Unable to create '.git/index.lock': No space left on devicedisk fullfree space, check inodes
fatal: index file smaller than expectedindex truncated by a crashrm .git/index && git reset
error: could not lock config file .git/configconfig.lock presentremove after checking

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Identify the holder before deleting

bash
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.lock

An 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:

bash
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 --abort

Removing 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: false

and, 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:

bash
GIT_OPTIONAL_LOCKS=0 git status

Set 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

bash
df -h . # space df -i . # inodes mount | grep -E 'nfs|cifs|9p' touch .git/permcheck && rm .git/permcheck && echo writable

NFS 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:

bash
git diff --cached --stat git ls-files -s | wc -l

If 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 documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.