The 'fatal: invalid object name' error occurs when Git cannot find a referenced commit, branch, or object in the repository. This typically happens with empty repos, typos in references, or corrupted Git objects.
The "fatal: invalid object name" error in Git indicates that you've referenced something (a commit SHA, branch name, tag, or other Git object) that Git cannot locate in its object database. In Git, every piece of data is stored as an "object" - commits, trees (directories), blobs (file contents), and tags all have unique SHA-1 hashes that identify them. When you run commands like `git diff abc1234`, `git checkout feature-branch`, or `git show HEAD~5`, Git looks up these references in its object database. If the referenced object doesn't exist, Git returns this fatal error. This error is particularly common in three scenarios: when working with a freshly initialized repository that has no commits yet, when there's a typo in a commit hash or branch name, or when Git objects have been corrupted or deleted. Understanding which scenario applies to your situation is key to resolving the error.
First, check if the object or reference you're trying to use actually exists:
# List all local branches
git branch -a
# List all tags
git tag -l
# Check if a specific commit exists
git cat-file -t abc1234
# View recent commit history
git log --oneline -10If git cat-file -t returns "fatal: Not a git repository" or an error, the reference doesn't exist. Verify you've spelled the branch name or commit SHA correctly.
If you just ran git init and are trying to create a branch, you'll get this error because there are no commits yet. Create your first commit:
# Create a file (if none exist)
echo "# My Project" > README.md
# Stage all files
git add .
# Create the initial commit
git commit -m "Initial commit"
# Now you can create branches
git branch feature-branch
git checkout -b another-featureThe default branch (main or master) only exists after the first commit.
If you're referencing a remote branch like origin/main but getting an error, you may need to fetch it first:
# Fetch all remote branches
git fetch origin
# Or fetch a specific branch
git fetch origin main
# List remote tracking branches
git branch -r
# Now you can reference the remote branch
git diff origin/main
git checkout -b local-main origin/mainAfter fetching, remote tracking branches will be available for comparison and checkout.
Git commit hashes are case-sensitive and must be exact. Verify your hash:
# View recent commits with full hashes
git log --format="%H %s" -10
# Use tab completion (in most shells) to avoid typos
git show <TAB>
# Search for commits containing a partial message
git log --grep="your search term" --onelineYou can use abbreviated commit hashes (minimum 4 characters), but they must uniquely identify a commit. If you copied a hash from elsewhere, ensure no extra characters were included.
If you suspect corruption, use Git's file system check:
# Run basic integrity check
git fsck
# Run a more thorough check
git fsck --full
# Check for dangling objects
git fsck --no-dangling --name-objectsThis will report any corrupted, missing, or dangling objects. Common issues include:
- "broken link" - a commit references a missing object
- "dangling commit" - a commit not reachable from any branch
- "invalid sha1 pointer" - corrupted reference
If git fsck found corrupted objects, try these repair strategies:
# If you have a remote, fetch missing objects
git fetch --all
# Rebuild a specific corrupted file's object
git hash-object -w path/to/file
# Remove and re-fetch (if remote exists)
rm -rf .git/objects/pack
git fetch --allFor more severe corruption:
# Clone a fresh copy and replace .git folder
git clone <remote-url> fresh-clone
cp -r fresh-clone/.git .git-backup
rm -rf .git
mv .git-backup .git
git checkout .If you accidentally initialized Git inside an existing repo, you may have conflicting object databases:
# Find all .git directories
find . -name ".git" -type d
# Should only show ./.git for a normal repositoryIf you find nested .git directories:
# Remove the nested .git (be careful!)
rm -rf nested-folder/.git
# Re-add the folder's contents to the parent repo
git add nested-folder/
git commit -m "Add nested folder contents"Ensure you're using Git references correctly, not file paths where references are expected:
# Wrong: Using file path as first argument to reset
git reset C:/path/to/file # Error: invalid object name 'C'
# Correct: Use -- to separate refs from paths
git reset HEAD -- path/to/file
# Wrong: Branch name with special characters
git checkout my branch # Tries to checkout 'my' then 'branch'
# Correct: Quote branch names with spaces (though avoid spaces)
git checkout "my branch"For git diff, always use -- before file paths:
git diff HEAD -- path/to/file
git diff abc1234..def5678 -- src/### Understanding Git Objects
Git stores all data as objects in the .git/objects directory. Each object has a unique 40-character SHA-1 hash:
- Blob: File contents
- Tree: Directory structure pointing to blobs and other trees
- Commit: Snapshot pointing to a tree, parent commits, and metadata
- Tag: Named reference to a commit (for annotated tags)
When you reference abc1234, Git looks for an object starting with that hash. The object must exist and be reachable for the command to succeed.
### Sync Tool Corruption
Cloud sync tools (Dropbox, OneDrive, Google Drive, iCloud) can corrupt Git repositories by:
- Syncing partial file writes
- Creating conflicting copies of objects
- Deleting objects that appear "unused"
If you experience this:
1. Move your repositories outside synced folders
2. Use Git hosting (GitHub, GitLab) instead of sync tools
3. Add .git to the sync tool's exclusion list
### Recovering Lost Commits
If git fsck shows "dangling commits", you can recover them:
# Find dangling commits
git fsck --lost-found
# Recovered objects are in .git/lost-found/
# Inspect a dangling commit
git show <dangling-sha>
# Create a branch pointing to the recovered commit
git branch recovered-work <dangling-sha>### Shallow Clones
Shallow clones (git clone --depth=1) don't have full history. Referencing old commits will fail:
# Convert shallow clone to full clone
git fetch --unshallow
# Or fetch specific history depth
git fetch --depth=100kex_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