git index.lock file exists: Safe Fix by Root Cause (2026)

Published March 2, 2026 · 8 min read

Fix It With a Tool

Snapshot branch state before lock-file cleanup

Capture diff and commit intent first so lock-file recovery does not hide unrelated local changes.

Intent match: Use this page for errors such as fatal: Unable to create '.git/index.lock': File exists and recovery decisions where deleting lock files blindly could cause data loss.

Table of contents

  1. Root-cause classifier
  2. Stale process lock recovery
  3. Permission mismatch recovery
  4. Tool/process concurrency recovery
  5. Post-fix verification
  6. FAQ

1. Classify before deleting lock files

Three root-cause classes cover most index.lock incidents. Match your symptom first, then use the command path for that class.

Root-cause class Typical signals Safe first command
Stale process Terminal closed or crash during Git operation, then repeated lock-file error ps -ef | rg "[g]it"
Permission mismatch Permission denied on .git/index or lock creation after using sudo ls -ld .git .git/index .git/index.lock 2>/dev/null
Tool/process concurrency IDE + terminal + hooks running Git at the same time lsof .git/index.lock

2. Root cause: stale process

Use this branch when a prior Git process likely ended abnormally, leaving a stale lock file.

# verify active Git processes
ps -ef | rg "[g]it"

# inspect lock file
ls -la .git/index.lock

# only if no active Git process exists
rm -f .git/index.lock

# confirm repository state is readable again
git status -sb
Safety rule: Never remove .git/index.lock while an active Git write operation is still running.

3. Root cause: permission mismatch

Use this branch when lock-file creation fails because ownership or mode on .git files is inconsistent.

# inspect owner/group + modes
ls -ld .git .git/index .git/index.lock 2>/dev/null

# restore ownership to your current user
sudo chown -R "$USER":"$USER" .git

# ensure user write access to index
test -f .git/index && chmod u+rw .git/index

# retry a read/write Git command
git status -sb
Prevention: avoid mixing sudo git ... with normal user Git commands in the same repository.

4. Root cause: tool/process concurrency

Use this branch when multiple tools touch the repository concurrently (IDE background fetch, terminal rebase, hooks, CI helpers).

# identify lock owner process
lsof .git/index.lock

# pause competing Git clients (IDE auto-fetch, file watcher hooks)
# then rerun one command path at a time
git fetch --all --prune
git status -sb

# if lock persists with no owner, remove once and retry
rm -f .git/index.lock
git status -sb

If concurrency involved linked worktrees, continue with worktree collision recovery for index.lock. If push recovery is next, continue with non-fast-forward push recovery.

5. Post-fix verification checklist

# 1) lock file is gone
test ! -f .git/index.lock && echo "lock cleared"

# 2) index operations succeed
git add -A
git status -sb

# 3) branch mapping still correct
git branch -vv
git remote -v

FAQ

Can I always remove index.lock as the first step?

No. Confirm the root-cause class first. Removing it during an active process can corrupt index updates.

What if permission errors keep returning?

Audit ownership drift in your repo workflow (sudo, container volume mounts, shared user contexts) and normalize ownership of the entire .git directory.

How do I reduce recurring lock races in teams?

Avoid running overlapping Git operations from multiple clients. Disable aggressive IDE background fetch during rebases and large merges.

Use deterministic recovery, then continue normal Git workflow. If a lock incident happened during deployment changes, validate infra state before retrying deploy steps.