How to fix error: maintenance gc task failed in Git
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.
Quick fix
Fixes error: maintenance gc task failed
cat .git/gc.log- Check for gc.log file and review the error
- Close programs that might lock Git files
- Check and fix repository permissions
- Run repository integrity check
cat .git/gc.logerror: maintenance gc task failed
What this error means
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.
Typical symptoms
- Push or pull operations show "maintenance gc task failed" warning
- Repository size grows larger than expected over time
- Automatic garbage collection stops working
- A gc.log file appears in the .git directory
- Git operations become slower due to fragmented repository
- Windows processes like TortoiseGit, Visual Studio, or IISExpress lock repository files
Common causes
- File locking by IDE or Git GUI tools (TortoiseGit, Visual Studio, Eclipse with EGit)
- Existing gc.log file blocking automatic garbage collection
- Insufficient file permissions to modify .git directory
- Corrupted objects in the repository database
- Insufficient disk space for repacking operations
- Concurrent Git operations interfering with garbage collection
- Memory limits exceeded on large repositories (especially on Windows)
- Dangling or unreachable objects causing cleanup failures
How to fix "error: maintenance gc task failed"
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 --refreshAdvanced notes
git 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.
Frequently asked questions
- What does "error: maintenance gc task failed" mean?
- 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.
- How do I fix "error: maintenance gc task failed"?
- 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.
- What causes "error: maintenance gc task failed"?
- File locking by IDE or Git GUI tools (TortoiseGit, Visual Studio, Eclipse with EGit) Existing gc.log file blocking automatic garbage collection Insufficient file permissions to modify .git directory
- Is the "error: maintenance gc task failed" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Git Official Documentation
- ๐Docs: git maintenance(Official Docs)
- ๐Docs: git gc(Official Docs)