The 'fatal: missing tree' error occurs when Git cannot find a tree object referenced by a commit or another tree. This indicates repository corruption requiring recovery from backups or remote sources.
The "fatal: missing tree" error in Git indicates that your repository is missing a critical tree object that is referenced by a commit or another tree object. In Git's internal data model, a tree object represents a directory snapshot - it maps file names and subdirectory names to their corresponding blob (file content) and tree (subdirectory) objects. When Git stores your project history, it creates three types of objects: blobs (file contents), trees (directory structures), and commits (snapshots linking to root trees with metadata). Each commit points to a "root tree" representing the entire project directory at that moment. If any tree object in this chain is missing, Git cannot reconstruct the directory structure and reports this fatal error. This error is particularly concerning because tree objects are essential for Git to understand your project's file structure. Unlike a missing blob (which only affects a single file), a missing tree can make an entire directory - or even your whole repository - unreadable. The error message includes a partial SHA-1 hash (like "abc1234") identifying which specific tree object Git cannot find.
First, run a filesystem check to understand the extent of the corruption:
# Run a full filesystem check
git fsck --full
# For more detailed output
git fsck --full --verboseThis will list all missing or corrupted objects. Note down the SHA-1 hashes of missing trees - you'll need these for recovery. Look for lines like:
- missing tree abc1234...
- broken link from tree def5678 to tree abc1234
- broken link from commit 123abcd to tree abc1234
Before attempting any repairs, back up your repository to prevent further data loss:
# Create a backup of the entire repository
cp -r /path/to/your/repo /path/to/your/repo-backup
# Or use tar for a compressed backup
tar -czvf repo-backup.tar.gz /path/to/your/repoThis ensures you can retry recovery if something goes wrong during repair attempts.
If you have a remote repository (GitHub, GitLab, etc.), try fetching the missing objects:
# Fetch all objects from the remote
git fetch --all
# Or fetch with full depth if it's a shallow clone
git fetch --unshallowIf the standard fetch doesn't work, try fetching specific missing objects:
# Get the list of missing objects and try to fetch them
git fsck --lost-found 2>&1 | grep "missing tree" | awk '{print $3}' | while read hash; do
git fetch origin $hash || echo "Could not fetch $hash"
doneClone a fresh copy and copy objects from it:
# Clone a fresh copy to a temporary location
git clone <your-remote-url> /tmp/fresh-clone
# Copy all objects from the fresh clone to your corrupted repo
cp -r /tmp/fresh-clone/.git/objects/* /path/to/corrupted/repo/.git/objects/Alternatively, unpack objects from the fresh clone's pack files:
# Navigate to your corrupted repository
cd /path/to/corrupted/repo
# Unpack objects from the fresh clone
git unpack-objects < /tmp/fresh-clone/.git/objects/pack/pack-*.packAfter copying, verify the fix:
git fsck --fullIf you have a backup of your repository, restore the missing objects:
# Copy objects from backup to corrupted repo
cp -r /path/to/backup/.git/objects/* /path/to/corrupted/repo/.git/objects/
# Merge objects directories (preserving existing)
rsync -av /path/to/backup/.git/objects/ /path/to/corrupted/repo/.git/objects/After restoration, verify and repack:
# Check repository integrity
git fsck --full
# Clean up and optimize if fsck passes
git gc --prune=nowIf remote recovery fails and you have uncommitted work, you can rebuild from your working directory:
# Save your current working directory
cp -r /path/to/repo /path/to/working-copy-backup
# Remove corrupted .git and reinitialize
cd /path/to/repo
rm -rf .git
git init
git remote add origin <your-remote-url>
# Fetch from remote
git fetch origin
# Reset to remote branch, keeping your local files
git reset --mixed origin/main
# Review changes and commit
git status
git add .
git commit -m "Recovered working directory after repository corruption"Warning: This preserves your working directory but loses local-only commit history.
If cloud sync services caused the corruption, prevent future issues:
For Dropbox:
Create a .dropboxignore file or move the repository outside the Dropbox folder.
For OneDrive:
# Windows: Exclude .git from OneDrive sync
attrib +U -P /path/to/repo/.git /S /DFor any cloud sync:
# Move repository outside sync folder
mv ~/Dropbox/project ~/projects/project
# Or create repository outside and symlink (not .git)
ln -s ~/projects/project/src ~/Dropbox/project-srcBest practice: Never store Git repositories inside folders synced by cloud services.
After successful recovery, verify repository integrity and clean up:
# Final integrity check
git fsck --full --strict
# Run garbage collection to optimize
git gc --prune=now --aggressive
# Verify you can access history
git log --oneline -20
# Test checking out different branches
git branch -a
git checkout mainIf git fsck reports no errors and you can browse history normally, your repository is recovered.
### Understanding Git Object Types
Git uses four object types internally:
- Blob: Stores file contents (binary data)
- Tree: Maps filenames to blobs and subdirectory trees (like a directory listing)
- Commit: Points to a root tree plus parent commits, author info, and message
- Tag: Points to a specific commit with optional metadata
When you see "missing tree," Git is saying a directory snapshot referenced by a commit (or another tree) doesn't exist in the object database.
### The .git/objects Directory Structure
Git stores objects in .git/objects/ using the first 2 characters of the SHA-1 hash as a subdirectory name:
.git/objects/
ab/
c123456789... (object with hash abc123...)
pack/
pack-xxx.pack (compressed pack file)
pack-xxx.idx (pack index)Loose objects are individual files. Pack files are compressed collections created by git gc or git repack.
### Identifying What the Missing Tree Contained
To understand what you've lost, examine which commits reference the missing tree:
# Find commits referencing the missing tree
git log --all --pretty=format:"%H" | while read commit; do
git ls-tree -r $commit 2>/dev/null | grep "MISSING_HASH" && echo "Found in commit: $commit"
doneReplace MISSING_HASH with your actual missing tree hash.
### Using git-repair (Third-Party Tool)
The git-repair tool from git-annex can automate some recovery steps:
# Install git-repair (Debian/Ubuntu)
sudo apt-get install git-repair
# Run repair
git-repair --force### Preventing Future Corruption
1. Use proper shutdown procedures - don't kill Git processes mid-operation
2. Monitor disk health - use SMART monitoring for early warning of disk failures
3. Regular backups - use git bundle or mirror clones for local backups
4. Avoid cloud sync - never put .git directories in Dropbox, OneDrive, etc.
5. Use `git fsck` periodically - add it to your maintenance routine
fatal: bad object in rev-list input
Git rev-list encounters bad or invalid object
fatal: Out of memory, malloc failed during pack operation
Out of memory during Git pack operation
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