Home › Git

fatal: not a valid object name: 'main' (Git) — how to fix

fatal: not a valid object name: 'main'
fatal: Not a valid object name main

Quick fix: Check what exists: git branch -a and git log --oneline -1. In a new repo, make a first commit before creating branches (git add . && git commit -m "Initial commit"). Use the real branch name, or for a remote-only branch run git fetch origin and use origin/main.

What it means

Commands like git branch new-feature main or git ls-tree main need to turn the name you gave into a commit or tree. "Not a valid object name" means Git looked in local branches, tags and other refs and found nothing with that name.

Other commands word the same problem differently: git checkout -b x main says fatal: 'main' is not a commit and a branch 'x' cannot be created from it, and git worktree add says fatal: invalid reference. The fixes below apply to all of them.

See what does exist:

git branch -a          # local and remote-tracking branches
git tag
git log --oneline -1   # does the current branch have any commit at all?

Cause 1: the repository has no commits yet

git init
git branch dev
# fatal: Not a valid object name: 'master'.

A fresh repository's branch points at nothing until the first commit, so there's nothing to branch from. Commit first:

git add .
git commit -m "Initial commit"
git branch dev

If you only want to start on a different branch name, rename the unborn branch instead:

git checkout -b dev        # works on an unborn branch
# or
git branch -m dev

Cause 2: main vs master

Older Git versions and many existing repos use master; GitHub and newer setups use main. Using the wrong one triggers the error:

git branch -a
git symbolic-ref --short HEAD        # current branch name

Use the actual name, or rename it:

git branch -m master main

Set the default for new repositories (Git 2.28+):

git config --global init.defaultBranch main

Cause 3: the branch exists only on the remote

After cloning, you get one local branch; others exist only as origin/<name>. If a branch was created on the server after you cloned, you don't even have that yet:

git fetch origin
git branch new-feature origin/develop
# or create a local tracking branch
git switch develop

In CI with shallow or single-branch clones (--depth 1, GitHub Actions actions/checkout default), other branches aren't fetched. Fetch the one you need explicitly:

git fetch origin main:refs/remotes/origin/main
git diff origin/main...HEAD

Cause 4: typo, case or a missing tag

Ref names are case-sensitive on Linux (Main is not main). Tags must be fetched to be usable: git fetch --tags. For a commit hash, make sure it's long enough to be unique and that the commit exists locally (git cat-file -t <hash> should print commit).

Last updated 2026-12-03.