How to fix error: could not lock config file .git/config: File exists in Git

GitBEGINNERLOWUpdated Apr 12, 2026ยท 235 views

This error occurs when Git cannot acquire a lock on its config file, usually because a .lock file was left behind by a crashed Git process or another application is holding the file open.

Quick fix

Fixes error: could not lock config file .git/config: File exists

# Check for lock file in the repository
ls -la .git/config.lock
  1. Locate and verify the lock file exists
  2. Check for running Git processes
  3. Delete the stale lock file
  4. Retry your Git command
git โ€” error
# Check for lock file in the repository
ls -la .git/config.lock
error: could not lock config file .git/config: File exists

What this error means

When Git modifies configuration files (like `.git/config` or `~/.gitconfig`), it uses a locking mechanism to prevent concurrent modifications that could corrupt the file. Git creates a temporary lock file by appending `.lock` to the config file name (e.g., `.git/config.lock` or `.gitconfig.lock`). The "could not lock config file: File exists" error means Git detected that a lock file already exists when it tried to create one. This typically happens when: 1. A previous Git operation was interrupted (crashed, killed, or lost connection) 2. Another Git process is currently modifying the config 3. An IDE or Git GUI tool is holding the file open 4. The lock file was orphaned due to a system crash or power failure The lock file mechanism is designed to protect your configuration from corruption, but when a lock is left behind incorrectly, it prevents all subsequent Git config operations.

Typical symptoms

  • Git commands that modify config fail with 'could not lock config file: File exists'
  • Unable to run 'git config' commands to set user name, email, or other settings
  • Git push or pull fails when trying to update remote tracking configuration
  • Error appears suddenly after a Git command was interrupted or system crashed
  • The error references either .git/config (local) or ~/.gitconfig (global)

Common causes

  1. A previous Git process crashed or was killed before removing its lock file
  2. Another Git process is currently running and holding the lock
  3. An IDE, Git GUI client, or editor plugin has the config file locked
  4. System crashed or lost power during a Git operation
  5. Multiple terminal sessions running conflicting Git commands simultaneously
  6. File system issues or network drive latency (for repos on network shares)

How to fix "error: could not lock config file .git/config: File exists"

1Locate and verify the lock file exists

First, confirm the lock file exists and identify its location. The error message will tell you which config file is affected.

For local repository config:

bash
# Check for lock file in the repository
ls -la .git/config.lock

For global user config:

bash
# Check for global config lock file
ls -la ~/.gitconfig.lock

# On Windows, check:
ls -la "$HOME/.gitconfig.lock"

If the file exists, you'll see it listed. If not, the issue may be with file permissions or another process.

2Check for running Git processes

Before deleting the lock file, verify no Git process is currently using it.

On Linux/macOS:

bash
# List all running Git processes
ps aux | grep git

# More specifically, look for processes that might hold the config
pgrep -a git

On Windows (Command Prompt):

cmd
tasklist | findstr git

On Windows (PowerShell):

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

If you find Git processes running, either wait for them to complete or terminate them before proceeding:

bash
# Kill a specific process by PID (Linux/macOS)
kill <PID>

# Force kill if needed
kill -9 <PID>
3Delete the stale lock file

Once you've confirmed no active Git process is using the lock, safely remove it.

For local repository config lock:

bash
# Remove the local config lock file
rm .git/config.lock

For global config lock:

bash
# Remove the global config lock file
rm ~/.gitconfig.lock

On Windows:

cmd
# Local config
del .git\config.lock

# Global config
del %USERPROFILE%\.gitconfig.lock

Important: Only delete the .lock file, never delete the actual config file (.git/config or .gitconfig).

4Retry your Git command

After removing the lock file, retry the command that originally failed.

bash
# Example: setting git config
git config user.name "Your Name"
git config user.email "your.email@example.com"

# Or whatever command triggered the error
git pull
git push

If the command succeeds, the issue is resolved. If the error recurs immediately, proceed to check for other processes or permission issues.

5Close IDEs and Git GUI clients

IDEs and Git GUI tools often run background Git operations that can hold locks. Close them temporarily to resolve persistent lock issues.

Common applications that may hold Git locks:
- Visual Studio Code (with Git extensions)
- IntelliJ IDEA / WebStorm / PyCharm
- Visual Studio
- GitKraken, SourceTree, GitHub Desktop
- Tower, Fork, or other Git clients

Steps:
1. Save your work in all applications
2. Close all IDEs and Git GUI clients
3. Delete the lock file again if it reappeared
4. Run your Git command
5. Reopen your applications

For VS Code specifically:

bash
# Kill VS Code processes (Linux/macOS)
pkill -f "code"

# Then remove lock and retry
rm .git/config.lock
git config user.name "Your Name"
6Check and fix file permissions

If you still cannot create the lock file, there may be permission issues.

Check ownership of the .git directory:

bash
# View ownership
ls -la .git/

# Check specifically the config file
ls -la .git/config

Fix ownership if needed (Linux/macOS):

bash
# Change ownership of .git directory to your user
sudo chown -R $(whoami) .git/

# Or fix just the config file
sudo chown $(whoami) .git/config

For global config:

bash
# Check ownership
ls -la ~/.gitconfig

# Fix if owned by root
sudo chown $(whoami):$(whoami) ~/.gitconfig

On Windows (Run as Administrator):

powershell
# Take ownership of the file
takeown /f .git\config
icacls .git\config /grant %USERNAME%:F
7Reboot as a last resort

If the lock file keeps reappearing or you cannot identify the process holding it, a system reboot will clear all file handles.

Before rebooting:

1. Save all your work
2. Close all applications properly
3. Commit or stash any uncommitted changes if possible

After rebooting:

bash
# Verify lock file is gone
ls -la .git/config.lock

# If it still exists, delete it
rm .git/config.lock

# Retry your Git command
git config --list

A reboot ensures all orphaned file handles from crashed processes are released.

8Handle network drive or filesystem issues

If your repository is on a network drive (NFS, SMB, CIFS), lock file issues can be caused by network latency or stale NFS handles.

For NFS mounts (Linux):

bash
# Check for stale NFS handles
ls -la .git/

# If you get "Stale file handle" errors, remount
sudo umount /path/to/mount
sudo mount /path/to/mount

For SMB/CIFS (Windows shares):

bash
# Disconnect and reconnect the network drive
net use Z: /delete
net use Z: \\server\share

Best practice for network drives:
Consider cloning repositories locally and using Git's remote features instead of working directly on network-mounted repositories. Git's file locking mechanism assumes local filesystem semantics that may not work reliably over network shares.

bash
# Clone to local disk instead
git clone /network/path/repo.git ~/local/repo
cd ~/local/repo
git remote add network /network/path/repo.git

Advanced notes

### How Git's Lock File Mechanism Works

Git uses a simple but effective file locking mechanism for configuration files. When Git needs to modify a config file:

1. It creates a lock file by appending .lock to the original filename
2. Writes the new content to the lock file
3. Atomically renames the lock file to the original filename
4. This ensures readers see either the old or new content, never a partial write

The lock file is automatically removed when:
- The Git process completes successfully (via rename)
- The Git process calls rollback_lock_file() to abort the change

Lock files are orphaned when Git is killed before cleanup or the system crashes.

### Preventing Future Lock Issues

1. Avoid killing Git processes - Let Git commands complete naturally when possible

2. Use Git hooks carefully - Long-running or failing hooks can leave locks behind:

bash
# Check for problematic hooks
ls -la .git/hooks/

3. Configure your IDE - Some IDEs aggressively poll Git status. Reduce polling frequency:
- VS Code: Set git.autoRefresh to false or increase the interval
- IntelliJ: Settings > Version Control > Git > Refresh file status

4. Script lock file cleanup - For CI/CD environments:

bash
# Add to your CI script before Git operations
find .git -name "*.lock" -type f -delete 2>/dev/null || true

### Lock Files in Bare Repositories

Bare repositories (like those on Git servers) have config files directly in the repository root:

bash
# Lock file location in bare repos
rm /path/to/repo.git/config.lock

### When Lock Errors Indicate Real Problems

If lock errors occur repeatedly without any obvious cause:

1. Check disk health - Failing disks can cause write issues
2. Verify filesystem integrity - Run fsck/chkdsk
3. Check for malware - Some malware interferes with developer tools
4. Review antivirus exclusions - Add .git directories to exclusions

### Alternative: Using Git's Built-in Lock Timeout

Git has a core.filesRefLockTimeout config that specifies how long to wait for a lock:

bash
# Wait up to 10 seconds for locks to be released
git config core.filesRefLockTimeout 10000

This can help in environments where brief lock contention is common.

Frequently asked questions

What does "error: could not lock config file .git/config: File exists" mean?
gitconfig), it uses a locking mechanism to prevent concurrent modifications that could corrupt the file. lock). The "could not lock config file: File exists" error means Git detected that a lock file already exists when it tried to create one.
How do I fix "error: could not lock config file .git/config: File exists"?
This error occurs when Git cannot acquire a lock on its config file, usually because a .lock file was left behind by a crashed Git process or another application is holding the file open.
What causes "error: could not lock config file .git/config: File exists"?
A previous Git process crashed or was killed before removing its lock file Another Git process is currently running and holding the lock An IDE, Git GUI client, or editor plugin has the config file locked
Is the "error: could not lock config file .git/config: File exists" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading