Skip to content

Fix: Git fatal: not a valid object name: 'main'

FixDevs ·

Quick Answer

How to fix Git fatal not a valid object name error caused by empty repositories, wrong branch names, missing initial commits, and corrupted refs.

The Error

You run a Git command and get:

fatal: not a valid object name: 'main'

Or variations:

fatal: not a valid object name: 'master'
fatal: not a valid object name: 'HEAD'
fatal: not a valid object name: 'origin/main'
error: pathspec 'main' did not match any file(s) known to git

Git cannot find the branch, commit, or ref you specified. The name does not point to any existing Git object.

Why This Happens

Git stores branches, tags, and commits as objects. When you reference a branch name like main, Git looks up its corresponding commit hash. If the branch does not exist or has never had a commit, Git cannot resolve the name.

Common causes:

  • Empty repository. You just ran git init but have not made any commits yet. No branches exist until the first commit.
  • Wrong branch name. You typed main but the branch is called master, or vice versa.
  • Remote branch not fetched. You reference origin/main but have not run git fetch yet.
  • Typo in the branch name. git checkout featur/login instead of feature/login.
  • Deleted branch. The branch was deleted locally or on the remote.
  • Corrupted repository. The .git directory has missing or corrupted objects.

Fix 1: Make an Initial Commit

The most common cause. A freshly initialized repository has no commits and no branches:

git init
git branch  # Shows nothing — no branches exist yet

Fix: Create the first commit:

git add .
git commit -m "Initial commit"

After this, the default branch (main or master) is created. Now git branch shows the branch name.

If you want the branch named main:

git init
git add .
git commit -m "Initial commit"
git branch -M main

Pro Tip: Configure Git to use main as the default branch name for all new repositories:

git config --global init.defaultBranch main

This avoids the main vs master confusion for future projects.

Fix 2: Check the Correct Branch Name

You might be using the wrong branch name. Check what branches exist:

# Local branches
git branch

# Remote branches
git branch -r

# All branches
git branch -a

If the output shows master but you typed main:

git checkout master

Or rename the branch:

git branch -M master main

Check the default branch on the remote:

git remote show origin

Look for the HEAD branch: line — that is the remote’s default branch.

Fix 3: Fetch Remote Branches

If the error mentions a remote branch like origin/main:

git fetch origin

This downloads all remote branches and refs. After fetching:

git checkout main  # Now works if origin/main exists

If the remote repository is empty (no commits), git fetch has nothing to download and origin/main does not exist.

Clone instead of init + remote add:

If you are setting up from an existing remote repository:

git clone https://github.com/user/repo.git
cd repo
git branch  # Shows the default branch

Cloning fetches all branches automatically. If the clone fails with authentication errors, see Fix: git permission denied publickey.

Fix 4: Fix git checkout or git switch Errors

When checking out a branch that does not exist locally but exists on the remote:

# This fails if the branch doesn't exist locally:
git checkout feature/login
# error: pathspec 'feature/login' did not match any file(s) known to git

# Fix — fetch first:
git fetch origin
git checkout feature/login  # Auto-creates local tracking branch

Or use git switch (Git 2.23+):

git fetch origin
git switch feature/login

Create a new branch:

git checkout -b feature/login
# or
git switch -c feature/login

The -b (or -c) flag creates the branch if it does not exist.

Fix 5: Fix git merge and git rebase Errors

When merging or rebasing, the target branch must exist:

git merge main
# fatal: not a valid object name: 'main'

Check available branches:

git branch -a

If the branch exists on the remote but not locally:

git fetch origin
git merge origin/main

Or create a local tracking branch first:

git checkout main
git checkout -  # Go back to previous branch
git merge main

For merge conflicts after successful merging, see Fix: git merge conflict.

Fix 6: Fix git log on Empty Repository

Running git log on a repository with no commits:

git init
git log
# fatal: your current branch 'main' does not have any commits yet

This is expected. There is nothing to show. Make a commit first (Fix 1).

git show also fails:

git show HEAD
# fatal: not a valid object name: 'HEAD'

HEAD does not exist until the first commit. After committing, HEAD points to the latest commit.

Fix 7: Fix Orphan Branch Issues

An orphan branch has no parent commit — it starts a completely new history:

git checkout --orphan new-branch
git log  # fatal: your current branch 'new-branch' does not have any commits yet

Fix: Commit on the orphan branch:

git checkout --orphan new-branch
git add .
git commit -m "Initial commit on new branch"

If you created an orphan branch accidentally and want to go back:

git checkout main

If this fails with a detached HEAD error, see Fix: git detached HEAD.

Common Mistake: Using git checkout --orphan thinking it creates a regular branch. Orphan branches are used for special purposes like gh-pages or starting a completely separate history. For a normal new branch, use git checkout -b branch-name.

Fix 8: Fix Corrupted Refs

If the branch existed previously and the error appeared suddenly, the ref might be corrupted:

Check the ref file:

cat .git/refs/heads/main

If the file is empty or contains garbage, the ref is corrupted.

Fix from the reflog:

git reflog

Find the last valid commit hash for the branch and recreate the ref:

git branch -f main abc1234

Fix from the remote:

git fetch origin
git branch -f main origin/main

Run git fsck to check for corruption:

git fsck --full

This checks all objects in the repository for consistency. If it reports errors, the repository might need repair from a fresh clone.

For similar Git reference issues, see Fix: git cannot lock ref.

Still Not Working?

If none of the fixes above resolved the error:

Check for case sensitivity. On Linux, Main and main are different branch names. On macOS and Windows, they are the same but Git can still get confused:

git branch -a | grep -i main

Check for special characters. Branch names with spaces, tildes, or colons are problematic. Rename the branch to use only alphanumeric characters, hyphens, and forward slashes.

Check bare repositories. If you are working in a bare repository (used as a server), there is no working directory and some commands behave differently. Check with git rev-parse --is-bare-repository.

Check submodules. If the error occurs inside a submodule, the submodule might be at a detached HEAD or an uninitialized state:

git submodule update --init --recursive

Try a fresh clone. If the local repository is corrupted beyond repair:

cd ..
mv myproject myproject-backup
git clone https://github.com/user/repo.git myproject

Then copy any uncommitted work from the backup.

If the error is specifically about the repository not being recognized as a Git repo at all, see Fix: fatal: not a git repository.

F

FixDevs

Solo developer based in Japan. Every solution is cross-referenced with official documentation and tested before publishing.

Was this article helpful?

Related Articles