Fixing Git’s “Could Not Lock Config File” Error on Windows
This error appears most often on Windows, where file‑locking is strict and background processes tend to hold references longer than expected. The full message usually looks like:
error: could not lock config file C:/Users/You/.gitconfig: File existsThis happens when Git tries to update configuration but detects a .gitconfig.lock file that shouldn’t exist — usually left behind by a crashed process or an application that is still holding the file open.
Below is an improved, extended guide on how to reliably fix this issue.
1. Close All Apps That Might Be Using Git
Before modifying .gitconfig, Git creates a lock file to prevent concurrent writes.
If anything else is touching your configuration, Git refuses to continue.
Close:
- VS Code
- JetBrains IDEs
- GitHub Desktop
- SourceTree
- Any terminal windows running Git commands
- Background Git clients (Git Credential Manager, extensions, plugins)
Then retry your Git command.
2. Delete the .gitconfig.lock File (Most Common Fix)
Navigate to:
C:\Users\<YourUser>\If you see:
.gitconfig.lockDelete it manually.
Or via terminal:
rm ~/.gitconfig.lockAfter removal, retry the Git operation.
⚠️ Important:
Delete only the .lock file — do not delete .gitconfig.
3. Check Which Process Is Locking the File (Advanced but Reliable)
Sometimes Windows keeps a handle open even after an app closes.
Use Resource Monitor:
- Press Win + R
- Run:
resmon.exe - Go to CPU → “Associated Handles” search
- Search:
plaintext
.gitconfig - Identify processes holding the lock
- Close them or stop them normally
Alternatively, using PowerShell (Windows 11):
Get-Process -Id (Get-FileLockInfo ~/.gitconfig).ProcessId(Requires the PowerShell Community Extensions module.)
4. Fix Permissions
Sometimes Git cannot create or remove the lock file because of access restrictions.
Check permissions:
Right‑click .gitconfig → Properties → Security
Ensure your user has:
- Read
- Write
- Modify
Or fix with PowerShell:
icacls "$env:USERPROFILE\.gitconfig" /grant "%USERNAME%":FThen retry your Git command.
5. Run the Terminal as Administrator
If permissions are restricted by corporate policy or file virtualization:
- Right‑click Command Prompt, PowerShell, or Git Bash
- Select Run as administrator
Retry your Git operation.
6. Ensure Your .gitconfig Is Not Read‑Only
Windows sometimes sets files as read-only (especially after backups).
Check:
Right-click .gitconfig → Properties → ensure “Read‑only” is unchecked.
Or via terminal:
attrib -r ~/.gitconfig7. Restart Your Computer (Releases Stuck File Handles)
If a process crashed and left phantom locks, a reboot clears the Windows handle table.
8. Delete Temporary Git Files in Case of Corruption
Sometimes the Git config directory itself contains stray temp files.
Check:
C:\Users\<You>\AppData\Local\Temp\Delete files starting with:
git-*These may reference old handles that prevent file cleanup.
9. For Global Git Config Issues in WSL
If you are using WSL and Git mixes Windows + Linux paths:
sudo rm ~/.gitconfig.lock
sudo chown $USER ~/.gitconfigAlso check /mnt/c/Users/<username>/.gitconfig.
Summary
| Problem | Cause | Fix |
|---|---|---|
.gitconfig.lock exists | Previous process crashed | Delete lock file |
Another app is using .gitconfig | IDEs / Git tools hold the file | Close apps or kill handles |
| No write permissions | Windows ACL issue | Fix permissions or run as admin |
| File marked read‑only | Windows attribute set | Remove read‑only attribute |
| WSL conflict | Mixed environments | Fix ownership & paths |