What's Actually Happening
A previous git operation (commit, add, merge, etc.) was interrupted before it could complete and clean up. Git creates a .git/index.lock file to prevent concurrent modifications to the index. If the process crashes or is killed, this lock file remains, blocking all subsequent git operations that need to modify the index.
The Error You'll See
When trying any git command:
fatal: Unable to create '.git/index.lock': File exists.Or:
``` fatal: Unable to create '/path/to/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 to let it complete before trying again. If this is not the case, please manually delete the file. ```
During commit:
error: Unable to create '.git/index.lock': File exists.
fatal: index file corruptDuring add:
fatal: Unable to create '.git/index.lock': File exists.
fatal: index file corruptWhy This Happens
The .git/index.lock file exists when:
- A git process was killed (Ctrl+C, kill command, system crash)
- An IDE or editor crashed while running git
- A git operation timed out
- System ran out of disk space during operation
- Another git process is legitimately running
- The operation was interrupted by shutdown
- Network issues during remote operations
Git creates this lock to ensure only one process modifies the index at a time. It should be deleted when the operation completes, but crashes leave it behind.
Step 1: Verify No Git Process is Running
First, ensure no git operation is actually in progress:
ps aux | grep gitOr more specifically:
pgrep -a gitIf you see a running git process:
# Wait for it to complete, or if it's stuck:
kill -9 <pid>On Windows:
tasklist | findstr git
taskkill /F /PID <pid>Step 2: Locate the Lock File
Find the lock file:
ls -la .git/index.lockFor bare repositories:
ls -la /path/to/repo.git/index.lockFor worktrees:
ls -la .git/worktrees/<name>/index.lockStep 3: Verify Index Integrity
Before removing the lock, check if the actual index is okay:
git statusIf this shows errors, the index might be corrupted:
git fsckStep 4: Remove the Lock File
If no git process is running and the index seems fine, remove the lock:
rm .git/index.lockOr with full path:
rm /path/to/repo/.git/index.lockOn Windows (PowerShell):
Remove-Item .git\index.lockOn Windows (Command Prompt):
del .git\index.lockStep 5: Verify the Fix
Try the blocked operation again:
git status
git add .
git commit -m "Test commit"All should work normally now.
Step 6: Handle Corrupted Index
If you still get errors after removing the lock:
rm .git/index
git resetOr recreate the index from HEAD:
git read-tree HEADStep 7: Recover Lost Changes
If you had staged changes that might be lost:
```bash # Check for dangling objects git fsck --lost-found
# Look for blob data ls .git/lost-found/other/ ```
Recover from .git/lost-found/:
# Each file is a lost object
for f in .git/lost-found/other/*; do
echo "=== $f ==="
git cat-file -t $(basename $f) 2>/dev/null && git cat-file -p $(basename $f) | head -20
doneStep 8: Prevent Future Locks
To avoid this issue:
- Don't kill git processes abruptly (let them finish or fail naturally)
- Ensure adequate disk space before large operations
- Check for background git processes before closing IDEs
- Use
git gcperiodically to clean up
Step 9: Handle Multiple Lock Files
Sometimes other lock files exist:
find .git -name "*.lock" -type fRemove stale locks:
find .git -name "*.lock" -type f -deleteBut be careful not to remove locks for running processes.
Step 10: Handle submodule Locks
For repositories with submodules:
```bash # Find submodule locks find .git/modules -name "*.lock" -type f
# Remove if stale find .git/modules -name "*.lock" -type f -delete ```
Verify the Fix
Confirm the lock is gone:
ls .git/index.lockShould return:
ls: cannot access '.git/index.lock': No such file or directoryTest all git operations:
git status
git log -1
git branch
git remote -vMake a test commit:
echo "test" > test.txt
git add test.txt
git commit -m "Test after lock removal"
git rm test.txt
git commit -m "Cleanup test file"All operations should complete without errors. The repository is now back to a normal working state.