Fixing Git’s “Could Not Lock Config File” Error on Windows

October, 30th 2024 3 min read

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:

plaintext
error: could not lock config file C:/Users/You/.gitconfig: File exists

This 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:

plaintext
C:\Users\<YourUser>\

If you see:

plaintext
.gitconfig.lock

Delete it manually.

Or via terminal:

bash
rm ~/.gitconfig.lock

After 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:

  1. Press Win + R
  2. Run: resmon.exe
  3. Go to CPU → “Associated Handles” search
  4. Search:
    plaintext
    .gitconfig
  5. Identify processes holding the lock
  6. Close them or stop them normally

Alternatively, using PowerShell (Windows 11):

powershell
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 .gitconfigPropertiesSecurity
Ensure your user has:

  • Read
  • Write
  • Modify

Or fix with PowerShell:

powershell
icacls "$env:USERPROFILE\.gitconfig" /grant "%USERNAME%":F

Then 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 .gitconfigProperties → ensure “Read‑only” is unchecked.

Or via terminal:

bash
attrib -r ~/.gitconfig

7. 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:

plaintext
C:\Users\<You>\AppData\Local\Temp\

Delete files starting with:

plaintext
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:

bash
sudo rm ~/.gitconfig.lock
sudo chown $USER ~/.gitconfig

Also check /mnt/c/Users/<username>/.gitconfig.


Summary

ProblemCauseFix
.gitconfig.lock existsPrevious process crashedDelete lock file
Another app is using .gitconfigIDEs / Git tools hold the fileClose apps or kill handles
No write permissionsWindows ACL issueFix permissions or run as admin
File marked read‑onlyWindows attribute setRemove read‑only attribute
WSL conflictMixed environmentsFix ownership & paths