This error occurs when Git cannot find the object (commit, branch, tag, or file) you referenced. The object name may be a typo, the repository may lack commits, or the reference simply does not exist.
In Git, every piece of data is stored as an "object" identified by a 40-character SHA-1 hash. There are four types of objects: blobs (file contents), trees (directories), commits (snapshots with metadata), and tags (named references to commits). When you run commands like `git show`, `git checkout`, `git branch`, or `git log` with a reference, Git looks up that name in its object database. The "fatal: Not a valid object name" error means Git searched for the specified reference but could not find any matching object. This can happen because the name doesn't exist, hasn't been created yet, or was typed incorrectly. Unlike "object not found" errors that indicate corruption, this error simply means the reference you provided doesn't resolve to anything Git knows about. This is one of the most common Git errors for beginners, especially when working with newly initialized repositories that have no commits yet, or when referencing remote branches that haven't been fetched.
First, verify that your repository actually has commits:
git log --onelineIf you see "does not have any commits yet", you need to make your first commit before you can reference branches:
git add .
git commit -m "Initial commit"After the first commit, 'master' or 'main' will exist.
Check what branches, tags, and references exist in your repository:
# List all local branches
git branch
# List all remote-tracking branches
git branch -r
# List all tags
git tag
# List all references
git show-refMake sure the name you're trying to use appears in one of these lists.
If you're trying to reference a remote branch like 'origin/main', you need to fetch it first:
# Fetch all branches from origin
git fetch origin
# Or fetch a specific branch
git fetch origin mainAfter fetching, 'origin/main' will be available as a valid object name.
Git object names are case-sensitive. Verify you're using the correct spelling:
# Check if a partial SHA exists
git rev-parse --verify abc1234
# Search for similar branch names
git branch --list '*main*'
git branch --list '*master*'Common mistakes include 'Main' vs 'main', 'master' vs 'main', or truncated SHAs that are too short to be unique.
If you cloned with --depth, older commits may be missing:
# Check if this is a shallow clone
git rev-parse --is-shallow-repository
# If true, fetch full history
git fetch --unshallow
# Or fetch more commits
git fetch --depth=100This is common in CI/CD pipelines that use shallow clones for speed.
In GitHub Actions, GitLab CI, or other CI systems, you often need to explicitly fetch branches:
# GitHub Actions example
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history
# Or manually fetch the base branch
- run: git fetch origin mainFor tools like Nx that compare against a base branch, ensure that branch is available:
git fetch origin main:mainIf the object should exist but Git can't find it, the repository may be corrupted:
# Verify repository integrity
git fsck --full
# Check for missing objects
git fsck --lost-foundIf objects are missing, you may need to re-clone the repository or restore from a backup.
Understanding Git Objects: Every Git object has a unique 40-character SHA-1 hash. You can reference objects by their full hash, a unique prefix (minimum 4 characters), branch name, tag name, or special references like HEAD, HEAD~1, or @{upstream}. The error occurs when none of these resolve to a valid object.
Shallow Clones in CI/CD: Many CI systems use shallow clones (git clone --depth 1) for speed. This means only the latest commit is available. When running commands that need to compare against other branches or commits (like git diff main...HEAD or monorepo tools), you must first fetch the required history.
The HEAD Reference: In a new repository before any commits, HEAD points to a branch that doesn't exist yet. Running git symbolic-ref HEAD shows 'refs/heads/main' or 'refs/heads/master', but that branch won't be valid until after the first commit.
Worktrees and Submodules: If you're working with git worktrees or submodules, ensure you're in the correct repository context. The error might occur because you're referencing an object from a different worktree or the parent repository.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server