KhueApps
Home/DevOps/Fix Git error: unable to create .git/index.lock (File exists)

Fix Git error: unable to create .git/index.lock (File exists)

Last updated: October 07, 2025

Overview

This guide fixes the Git error:

fatal: unable to create .../.git/index.lock: File exists

It happens when Git detects a lock on the index (often from an interrupted command, a concurrent Git process, or filesystem issues). Follow these steps to resolve it safely on Unix and Windows.

Quickstart

  • Ensure no Git process is running in this repo.
  • If safe, delete the lock file: .git/index.lock.
  • If a merge/rebase/cherry-pick was in progress, abort or continue appropriately.
  • Verify filesystem health: permissions, disk space, antivirus, network shares.

Minimal working example

Reproduce the error and fix it locally.

# Unix/macOS
mkdir demo-lock && cd demo-lock
git init
# Simulate a stale lock
: > .git/index.lock
# Any Git command now fails
git status  # -> fatal: unable to create .git/index.lock: File exists

# Fix: remove the stale lock
rm -f .git/index.lock
# Git works again
git status

Windows (PowerShell):

mkdir demo-lock; Set-Location demo-lock
git init
# Simulate a stale lock
New-Item .git/index.lock -ItemType File | Out-Null
# Fails now
git status
# Fix
Remove-Item .git/index.lock -Force
git status

Step-by-step fix

  1. Confirm if Git is still running
  • Unix/macOS:
ps aux | grep -E "[g]it|[s]sh|[g]it-" | grep -v grep
# Optional if available: who holds the file
lsof .git/index.lock 2>/dev/null || true
  • Windows:
tasklist | findstr /I git

If you see a relevant process (e.g., a long-running git, IDE-integrated Git, or pre-commit hook), wait for it to finish or close it.

  1. If safe, remove the lock file
  • Unix/macOS:
rm -f .git/index.lock
  • Windows (CMD):
del /F .git\index.lock
  • Windows (PowerShell):
Remove-Item .git/index.lock -Force
  1. Resume or abort any in-progress operation After removing the lock, one of these may apply:
In-progress stateHow to abortHow to continue
Mergegit merge --abortgit commit or resolve and git merge --continue
Rebasegit rebase --abortgit rebase --continue
Cherry-pickgit cherry-pick --abortgit cherry-pick --continue
Revertgit revert --abortgit revert --continue
Apply patches (am)git am --abortgit am --continue
  1. Verify repository health
git status
git fsck --no-dangling
  1. Prevent recurrence
  • Avoid running multiple Git operations simultaneously on the same repo (including IDE background tasks).
  • Exclude the .git directory from real-time antivirus if allowed.
  • Prefer local disks over flaky network shares for active work.

Worktrees and other lock locations

With git worktree, locks may live in:

  • Main worktree: .git/index.lock
  • Linked worktree: .git/worktrees/<name>/index.lock

Fix: remove the specific lock in the affected worktree, not only the main one.

# Example: remove a lock in a linked worktree
rm -f .git/worktrees/feature/index.lock

Submodules have their own .git directories. If the error appears while operating in a submodule path, remove the submodule's lock:

rm -f path/to/submodule/.git/index.lock

Common root causes and checks

  • Interrupted Git command (crash, Ctrl-C, power loss): remove lock and continue/abort.
  • IDE or background tool holding the index: close the tool or disable background VCS operations.
  • File permissions or ownership mismatch:
# Unix/macOS: fix ownership to current user
sudo chown -R "$USER" .git
# Ensure write permission
chmod -R u+rwX .git
  • Disk full or quota exceeded:
# Unix/macOS
df -h .
# Windows
wmic logicaldisk get caption,freespace,size
  • Networked filesystems (NFS/SMB) inconsistencies: move the repo to a local disk for active development.

Diagnostics cheat sheet

  • Identify lock file path from the error; remove exactly that file.
  • List dot-git locks:
find .git -maxdepth 2 -name "*.lock" -print 2>/dev/null || true
  • Confirm Git can write to the index after fix:
printf "test" > test.txt
git add test.txt && git reset -- test.txt

Pitfalls

  • Do not delete unrelated lock files blindly (e.g., HEAD.lock used by other operations) without understanding the context.
  • Do not habitually remove locks while a Git process is still running; you risk index corruption.
  • Avoid using sudo for normal Git work; it creates root-owned files that later block regular operations.
  • On Windows, editors with file indexing or antivirus may immediately recreate locks—temporarily pause them or exclude the repository.

Performance notes

  • Large repositories increase the chance and impact of lock contention. To reduce pressure:
    • Use sparse checkout for large monorepos:
      git sparse-checkout init --cone
      git sparse-checkout set path/you/need
      
    • Enable file system monitoring when available to speed up status/scan:
      git config core.fsmonitor true
      
    • Keep repos on SSDs/local disks; avoid high-latency network shares.
  • After resolving issues, optional cleanup can help:
    git gc --aggressive --prune=now
    

FAQ

Q: Is it safe to delete .git/index.lock? A: Yes, if no Git process is running. Always check for active processes first.

Q: The lock file won't delete on Windows. A: A process likely holds it open. Close IDEs/terminals using the repo, then retry. Reboot if necessary.

Q: I keep getting the lock error repeatedly. A: Check permissions, disk space, antivirus exclusions, and avoid concurrent Git commands across terminals/IDEs.

Q: Does this affect submodules or worktrees differently? A: Yes. Remove the lock in the specific submodule or worktree where the error occurs.

Q: Could my index be corrupted? A: Rarely. After unlocking, run git fsck; if issues persist, recreate the index:

mv .git/index .git/index.backup 2>/dev/null || true
git reset

Series: Git

DevOps