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.
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.
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:
# 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.lockOn Windows (Command Prompt):
tasklist | findstr gitOn Windows (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.
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.
Once you've confirmed no Git processes are running, remove the stale lock file:
On Linux/macOS:
rm -f .git/index.lockIf you get a permission denied error:
# Check file ownership
ls -la .git/index.lock
# Remove with sudo if necessary (and you own the repo)
sudo rm -f .git/index.lockOn Windows (Command Prompt):
del .git\index.lockOn Windows (PowerShell):
Remove-Item .git\index.lock -ForceOn Windows (Git Bash):
rm -f .git/index.lockAfter removing the lock, retry your Git command. It should work now.
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
# Create the file if it doesn't exist, then remove it
touch .git/index.lock
rm -f .git/index.lockSolution 2: Check for hidden files on Windows
dir /a .gitSolution 3: Navigate to the exact directory
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.
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:
# Save any uncommitted work
git stash list # See if you have stashes
# Or just note what files were modified
git statusAfter 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.
If you're using Git submodules, lock files can appear in each submodule's .git directory:
# 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.
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:
# Clean up before operations in CI
rm -f .git/index.lock
# Or reset the repository state
git reset --hard HEAD
git clean -fdxOther 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
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git