Git maintenance gc task failed when attempting to clean up and optimize your repository. This usually occurs due to file locks, permission issues, or corrupted objects preventing garbage collection from completing.
Git garbage collection (gc) is a maintenance task that cleans up unnecessary files and optimizes your repository for better performance. The "maintenance gc task failed" error indicates that Git was unable to complete this optimization process. This error commonly appears during automatic maintenance operations (triggered by git maintenance run --auto or older git gc --auto commands) or when explicitly running git maintenance run --task=gc. Git performs garbage collection to remove unreachable objects, repack files for efficiency, and reduce disk space usage. The failure can stem from various sources including file system locks (especially on Windows with IDEs or Git GUI tools running), permission issues preventing Git from modifying repository files, insufficient disk space, or corrupted objects in the repository. When gc fails, Git creates a gc.log file that blocks future automatic cleanup attempts until the underlying issue is resolved.
First, check if a gc.log file exists that might be blocking automatic garbage collection:
cat .git/gc.logReview the error message to understand what went wrong. If the issue has been resolved, delete the gc.log file:
rm .git/gc.logThis will allow automatic garbage collection to run again on future operations.
On Windows, close any programs that might have locks on repository files:
- Close Visual Studio, VS Code, or other IDEs
- Exit TortoiseGit or other Git GUI tools
- Stop IISExpress or local development servers
- Close file explorers browsing the repository
After closing these programs, try running garbage collection again:
git maintenance run --task=gcOr with the older command:
git gcVerify that you have proper permissions on the .git directory:
ls -la .gitIf permissions are incorrect, fix them (Linux/macOS):
chmod -R 755 .git
chown -R $(whoami) .gitOn Windows, right-click the .git folder, select Properties > Security, and ensure your user account has Full Control.
Check for corrupted objects in your repository:
git fsck --fullCheck the count of loose objects:
git count-objects -vIf corrupted objects are found, you may need to recover from a backup or clone the repository fresh and copy your working directory changes.
Run garbage collection with debug logging to see detailed error output:
GIT_TRACE=1 git maintenance run --task=gcOr capture errors to a file:
git gc 2> gc_error.log
cat gc_error.logThis will help identify the specific cause of the failure.
If normal garbage collection fails, try forcing it with more aggressive options:
git gc --aggressive --prune=nowThis tells Git to:
- Use more aggressive optimization (slower but more thorough)
- Immediately prune all dangling commits instead of waiting 2 weeks
Note: This operation can take significant time on large repositories.
If automated gc fails, try manual cleanup steps:
First, repack the repository:
git repack -adfThen prune loose objects:
git pruneFinally, refresh the index:
git update-index --refreshgit maintenance vs git gc: Modern Git (2.31+) introduced the git maintenance command as a replacement for git gc. The maintenance command provides better control and should not be mixed with git gc. If you're using git maintenance, avoid running git gc directly.
Disabling automatic gc: You can disable automatic garbage collection with git config --global gc.auto 0, but this is not recommended as it will cause repository size to grow and slow down operations over time. Only use this as a temporary workaround.
Large repositories: Repositories with large binary files or extensive history may experience slow or failing gc operations. Consider using Git LFS (Large File Storage) for binary files or tools like git-annex for very large files.
Memory limits on Windows: Git gc may hit the 4GB memory limit on Windows systems. Using git config gc.pruneExpire "2 weeks ago" can help by spreading the work across multiple gc runs.
Concurrent operations: Git gc running concurrently with other Git operations can cause failures or even repository corruption. The git maintenance command handles locking more safely.
Pack redundancy: After a failed gc, redundant packs may accumulate. Use git pack-redundant --all | xargs rm to identify and remove them, but be cautious as this command is considered experimental.
As a last resort: If the repository is severely corrupted and gc cannot be fixed, clone the repository fresh and copy the .git folder to your working directory, or simply reclone and migrate your uncommitted changes.
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