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.
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.
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:
# Check for lock file in the repository
ls -la .git/config.lockFor global user config:
# 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.
Before deleting the lock file, verify no Git process is currently using it.
On Linux/macOS:
# List all running Git processes
ps aux | grep git
# More specifically, look for processes that might hold the config
pgrep -a gitOn Windows (Command Prompt):
tasklist | findstr gitOn Windows (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:
# Kill a specific process by PID (Linux/macOS)
kill <PID>
# Force kill if needed
kill -9 <PID>Once you've confirmed no active Git process is using the lock, safely remove it.
For local repository config lock:
# Remove the local config lock file
rm .git/config.lockFor global config lock:
# Remove the global config lock file
rm ~/.gitconfig.lockOn Windows:
# Local config
del .git\config.lock
# Global config
del %USERPROFILE%\.gitconfig.lockImportant: Only delete the .lock file, never delete the actual config file (.git/config or .gitconfig).
After removing the lock file, retry the command that originally failed.
# Example: setting git config
git config user.name "Your Name"
git config user.email "[email protected]"
# Or whatever command triggered the error
git pull
git pushIf the command succeeds, the issue is resolved. If the error recurs immediately, proceed to check for other processes or permission issues.
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:
# Kill VS Code processes (Linux/macOS)
pkill -f "code"
# Then remove lock and retry
rm .git/config.lock
git config user.name "Your Name"If you still cannot create the lock file, there may be permission issues.
Check ownership of the .git directory:
# View ownership
ls -la .git/
# Check specifically the config file
ls -la .git/configFix ownership if needed (Linux/macOS):
# Change ownership of .git directory to your user
sudo chown -R $(whoami) .git/
# Or fix just the config file
sudo chown $(whoami) .git/configFor global config:
# Check ownership
ls -la ~/.gitconfig
# Fix if owned by root
sudo chown $(whoami):$(whoami) ~/.gitconfigOn Windows (Run as Administrator):
# Take ownership of the file
takeown /f .git\config
icacls .git\config /grant %USERNAME%:FIf 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:
# 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 --listA reboot ensures all orphaned file handles from crashed processes are released.
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):
# 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/mountFor SMB/CIFS (Windows shares):
# Disconnect and reconnect the network drive
net use Z: /delete
net use Z: \\server\shareBest 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.
# Clone to local disk instead
git clone /network/path/repo.git ~/local/repo
cd ~/local/repo
git remote add network /network/path/repo.git### 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:
# 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:
# 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:
# 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:
# Wait up to 10 seconds for locks to be released
git config core.filesRefLockTimeout 10000This can help in environments where brief lock contention is common.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server