This error occurs when Git maintenance tries to run but cannot acquire a lock because another maintenance process is already running or a stale lock file exists from a previous operation.
Git maintenance operations (like `git maintenance run` or `git gc`) use lock files to prevent multiple processes from modifying the repository's object database simultaneously. When Git tries to start a maintenance task, it creates a lock file (typically `.git/objects/maintenance.lock`) to signal that the operation is in progress. If this lock file already exists, Git assumes another process is running and fails with this error. This locking mechanism is essential for maintaining repository integrity. Without it, concurrent maintenance operations could corrupt the object database, leading to data loss or repository corruption. The error is Git's way of protecting your repository from potentially destructive race conditions. The issue commonly occurs in environments with automated maintenance tasks (like scheduled `git gc --auto`), CI/CD pipelines, or when using Git GUI tools that run background maintenance. It can also result from interrupted operations that failed to clean up their lock files.
Before removing any lock files, verify whether a maintenance operation is actually running:
# On Linux/macOS
ps aux | grep -i git
# On Windows (PowerShell)
Get-Process | Where-Object {/home/jonas/.claude/shell-snapshots/snapshot-bash-1764676531613-9a9t8a.sh.ProcessName -like '*git*'}If you see a git maintenance, git gc, or git repack process running, wait for it to complete naturally. Killing it may require lock file cleanup.
If no git processes are running, the lock file is likely stale and can be safely removed:
# Remove maintenance lock
rm -f .git/objects/maintenance.lock
# If there are other lock files
find .git -name '*.lock' -type f
# Remove all lock files (use with caution)
find .git -name '*.lock' -type f -deleteOn Windows:
del .git\objects\maintenance.lockAfter removing the lock file, retry your maintenance operation.
If you've been mixing git gc with git maintenance run, use the maintenance command exclusively:
# Instead of: git gc
# Use:
git maintenance run --task=gc
# Run all maintenance tasks
git maintenance run
# Schedule automatic maintenance (recommended)
git maintenance startThe git maintenance command takes proper locks, while git gc may not coordinate correctly with maintenance operations.
If the lock file resulted from a system crash, check your repository for corruption:
# Check repository integrity
git fsck
# If issues found, attempt repair
git fsck --full
# Rebuild index if needed
rm -f .git/index
git resetIf git fsck reports errors, you may need to recover from a backup or use more advanced repair techniques.
Git GUI applications and IDEs often run background maintenance tasks:
1. Close all Git GUI clients (GitKraken, SourceTree, GitHub Desktop, etc.)
2. Close IDEs with Git integration (VS Code, IntelliJ, etc.)
3. Wait 30 seconds for background processes to terminate
4. Check Task Manager/Activity Monitor to confirm no git processes remain
5. Retry your maintenance operation
You may need to disable automatic maintenance in these tools if conflicts persist.
Automated Maintenance Scheduling
Git 2.17+ supports scheduled maintenance via git maintenance start, which registers maintenance tasks with your system's task scheduler (cron, systemd, Windows Task Scheduler). If you're experiencing frequent lock conflicts, review your scheduled tasks:
# Register scheduled maintenance
git maintenance start
# Unregister if causing conflicts
git maintenance stop
# Custom schedule for specific tasks
git config maintenance.auto false
git config maintenance.strategy incrementalLock Coordination Between git gc and git maintenance
The git gc command and git maintenance run use different locking strategies. Git 2.48+ (Q1 2025) improves this with better hint messages when locks conflict. Avoid running both commands in scripts—choose one approach:
- Option 1: Use git maintenance run --task=gc exclusively
- Option 2: Use git gc exclusively and avoid maintenance commands
Long-Running Maintenance
Some repositories with large histories may have maintenance operations that take over an hour. If your scheduled tasks overlap:
1. Increase the interval between scheduled runs
2. Use incremental maintenance tasks instead of full gc: git maintenance run --task=incremental-repack
3. Monitor task duration: time git maintenance run
CI/CD Considerations
In CI/CD environments, disable automatic maintenance in favor of explicit, controlled maintenance:
git config gc.auto 0
git config maintenance.auto falseRun maintenance only at strategic points in your pipeline where conflicts won't occur.
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