How to fix fatal: Unable to create '/path/to/repo/.git/index.lock': File exists in Git

GitBEGINNERMEDIUMUpdated Dec 6, 2025ยท 452 views

This error occurs when Git cannot acquire a lock on the repository's index file because a lock file already exists. This usually means another Git process is running, or a previous Git process crashed without cleaning up. The fix is typically to remove the stale lock file after confirming no Git processes are active.

Quick fix

Fixes fatal: Unable to create '/path/to/repo/.git/index.lock': File exists

# List all Git processes
ps aux | grep git

# Check what process is using the lock file (if lsof is available)
lsof .git/index.lock

# Alternative using fuser
fuser .git/index.lock
  1. Check for running Git processes
  2. Close applications that use Git
  3. Remove the lock file
  4. Handle the 'file not found' paradox
git โ€” error
# List all Git processes
ps aux | grep git
# Check what process is using the lock file (if lsof is available)
lsof .git/index.lock
# Alternative using fuser
fuser .git/index.lock
fatal: Unable to create '/path/to/repo/.git/index.lock': File exists

What this error means

The "Unable to create index.lock: File exists" error indicates that Git cannot obtain an exclusive lock on your repository's index. The `.git/index.lock` file is Git's way of preventing multiple processes from modifying the staging area simultaneously, which could lead to repository corruption. When you run a Git command that modifies the index (like `git add`, `git commit`, `git checkout`, `git pull`, etc.), Git creates this lock file at the start and removes it when the operation completes. If the lock file already exists, Git assumes another process is working and refuses to proceed. The problem arises when: - **A previous Git operation crashed or was interrupted** (Ctrl+C, power loss, system crash), leaving the lock file behind - **Another Git process is genuinely running** in the same repository, perhaps in another terminal window or via an IDE - **A background Git integration** (VS Code, JetBrains IDEs, Git GUI tools) is holding the lock This is a safety mechanism, not a bug. Git prioritizes data integrity over convenience. The lock prevents race conditions that could corrupt your repository's index, which tracks what's staged for the next commit.

Typical symptoms

  • Any Git command that modifies the index fails with 'fatal: Unable to create .git/index.lock'
  • The error appears suddenly after a crash, forced shutdown, or interrupted command
  • Git commands worked fine moments ago but now all fail with this error
  • Multiple terminals or IDE integrations accessing the same repository
  • The error persists even after waiting or retrying the command
  • CI/CD pipelines fail with this error during concurrent Git operations

Common causes

  1. A previous Git command was interrupted (Ctrl+C) or crashed before completing
  2. Your system crashed or lost power during a Git operation
  3. An IDE or editor (VS Code, IntelliJ, etc.) is running background Git operations
  4. Another terminal has a Git command running in the same repository
  5. A Git GUI application is holding the lock (GitHub Desktop, GitKraken, etc.)
  6. A Git hook script is running or hung in the background
  7. Network issues during a remote operation caused Git to hang and get killed
  8. Concurrent CI/CD jobs accessing the same repository simultaneously

How to fix "fatal: Unable to create '/path/to/repo/.git/index.lock': File exists"

1Check for running Git processes

Before removing the lock file, verify no Git processes are currently active. Deleting the lock while Git is running can corrupt your repository.

On Linux/macOS:

bash
# List all Git processes
ps aux | grep git

# Check what process is using the lock file (if lsof is available)
lsof .git/index.lock

# Alternative using fuser
fuser .git/index.lock

On Windows (Command Prompt):

cmd
tasklist | findstr git

On Windows (PowerShell):

powershell
Get-Process | Where-Object { $_.ProcessName -like "*git*" }

If you find active Git processes, wait for them to complete or terminate them if they appear hung.

2Close applications that use Git

Many applications run Git commands in the background and may be holding the lock:

Common culprits:
- VS Code - Has built-in Git integration that auto-fetches
- JetBrains IDEs (IntelliJ, PyCharm, WebStorm) - Background VCS operations
- GitHub Desktop, GitKraken, Sourcetree - Git GUI applications
- Git hooks - Pre-commit hooks, husky, etc.

Steps:
1. Close your IDE or disable its Git integration temporarily
2. Close any Git GUI applications
3. Stop any file watchers that trigger Git operations

To disable VS Code's Git temporarily:
1. Open Settings (Ctrl+,)
2. Search for "git.enabled"
3. Uncheck the setting
4. Or add to settings.json: "git.enabled": false

After removing the lock, you can re-enable Git integration.

3Remove the lock file

Once you've confirmed no Git processes are running, remove the stale lock file:

On Linux/macOS:

bash
rm -f .git/index.lock

If you get a permission denied error:

bash
# Check file ownership
ls -la .git/index.lock

# Remove with sudo if necessary (and you own the repo)
sudo rm -f .git/index.lock

On Windows (Command Prompt):

cmd
del .git\index.lock

On Windows (PowerShell):

powershell
Remove-Item .git\index.lock -Force

On Windows (Git Bash):

bash
rm -f .git/index.lock

After removing the lock, retry your Git command. It should work now.

4Handle the 'file not found' paradox

Sometimes Git reports the lock file exists, but you can't find it when you try to delete it. This can happen due to:
- File system caching issues
- The file being hidden
- Different path interpretations on Windows

Solution 1: Touch and remove

bash
# Create the file if it doesn't exist, then remove it
touch .git/index.lock
rm -f .git/index.lock

Solution 2: Check for hidden files on Windows

cmd
dir /a .git

Solution 3: Navigate to the exact directory

bash
cd .git
ls -la index.lock
rm -f index.lock
cd ..

Solution 4: Use Git's internal directory
If the error shows a full path like /path/to/repo/.git/index.lock, make sure you're in the correct repository root directory.

5Restart your system if the lock persists

If removing the lock file doesn't work or it keeps reappearing, a system restart will:
- Terminate any hung or zombie Git processes
- Release all file locks
- Clear any file system caches

Before restarting:

bash
# Save any uncommitted work
git stash list  # See if you have stashes

# Or just note what files were modified
git status

After rebooting:
1. Don't open your IDE immediately
2. Navigate to the repository in terminal
3. Verify the lock is gone: ls .git/index.lock
4. Try your Git command
5. Then open your IDE

This nuclear option works when subtle process issues are hard to diagnose.

6Handle locks in Git submodules

If you're using Git submodules, lock files can appear in each submodule's .git directory:

bash
# Find all index.lock files in the project
find . -name "index.lock" -type f

# Remove all lock files (use with caution!)
find . -name "index.lock" -type f -delete

# Or remove them one by one after inspection
find . -name "index.lock" -type f -exec ls -la {} \;

For submodules, the lock file might be in:
- .git/modules/submodule-name/index.lock
- submodule-path/.git/index.lock (if not using --reference)

Always check no Git operations are running in any submodule before removing locks.

Advanced notes

Understanding the Index Lock Mechanism:

Git's index (also called the staging area or cache) is stored in .git/index. This file is critical - it tracks what will go into your next commit. The index.lock file is Git's mutex:

1. Git creates .git/index.lock at operation start
2. Git writes the new index state to this lock file
3. Git atomically renames index.lock to index on success
4. If anything fails, the lock file remains and the index is unchanged

This atomic rename ensures the index is never corrupted, even during crashes.

Why Does the Lock Get Stuck?

The lock remains when Git doesn't complete its normal cleanup:
- SIGKILL (kill -9): Doesn't allow cleanup handlers to run
- System crash: No cleanup possible
- Process hang: Process still "alive" but not doing work
- Deadlock: Waiting for a resource that will never be available

A normal Ctrl+C (SIGINT) usually allows cleanup, but not always.

Concurrent Git Operations:

While the lock prevents most concurrent modifications, some Git commands are safe to run concurrently:
- git status (read-only)
- git log (read-only)
- git diff (read-only)
- git blame (read-only)

Commands that need the lock include:
- git add, git rm (modify index)
- git commit (reads and updates index)
- git checkout (updates index and working tree)
- git pull, git merge, git rebase (complex operations)

CI/CD Considerations:

In CI/CD environments, this error often indicates:
1. Parallel jobs sharing a workspace: Use separate workspaces or lock mechanisms
2. Stale workspaces: Clean workspace between builds
3. Aggressive job cancellation: CI runners killing Git mid-operation

Prevention strategies:

bash
# Clean up before operations in CI
rm -f .git/index.lock

# Or reset the repository state
git reset --hard HEAD
git clean -fdx

Other Lock Files:

Git uses several lock files, not just index.lock:
- .git/refs/heads/branch.lock - Branch reference updates
- .git/HEAD.lock - HEAD pointer updates
- .git/config.lock - Config file modifications
- .git/COMMIT_EDITMSG.lock - During commit message editing

The same diagnosis and removal approach works for all of these.

Platform-Specific Issues:

Windows:
- Antivirus software can hold locks on files
- Windows Defender may scan .git files
- Add your repository folder to antivirus exclusions
- Windows file locking is stricter than Unix

macOS:
- Spotlight indexing can interfere
- Add .git to Spotlight privacy exclusions
- Time Machine backups can cause brief locks

Network Drives:
- Lock files may not work correctly on NFS, SMB, or other network filesystems
- Git was designed for local filesystems
- Consider using git worktree with local storage if you must use network drives

Frequently asked questions

What does "fatal: Unable to create '/path/to/repo/.git/index.lock': File exists" mean?
lock: File exists" error indicates that Git cannot obtain an exclusive lock on your repository's index. lock file is Git's way of preventing multiple processes from modifying the staging area simultaneously, which could lead to repository corruption. ), Git creates this lock file at the start and removes it when the operation completes.
How do I fix "fatal: Unable to create '/path/to/repo/.git/index.lock': File exists"?
This error occurs when Git cannot acquire a lock on the repository's index file because a lock file already exists. This usually means another Git process is running, or a previous Git process crashed without cleaning up. The fix is typically to remove the stale lock file after confirming no Git processes are active.
What causes "fatal: Unable to create '/path/to/repo/.git/index.lock': File exists"?
A previous Git command was interrupted (Ctrl+C) or crashed before completing Your system crashed or lost power during a Git operation An IDE or editor (VS Code, IntelliJ, etc.) is running background Git operations
Is the "fatal: Unable to create '/path/to/repo/.git/index.lock': File exists" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading