Fix: Git fatal: not a valid object name: 'main'
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 gitGit 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 initbut have not made any commits yet. No branches exist until the first commit. - Wrong branch name. You typed
mainbut the branch is calledmaster, or vice versa. - Remote branch not fetched. You reference
origin/mainbut have not rungit fetchyet. - Typo in the branch name.
git checkout featur/logininstead offeature/login. - Deleted branch. The branch was deleted locally or on the remote.
- Corrupted repository. The
.gitdirectory 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 yetFix: 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 mainPro Tip: Configure Git to use
mainas the default branch name for all new repositories:git config --global init.defaultBranch mainThis avoids the
mainvsmasterconfusion 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 -aIf the output shows master but you typed main:
git checkout masterOr rename the branch:
git branch -M master mainCheck the default branch on the remote:
git remote show originLook 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 originThis downloads all remote branches and refs. After fetching:
git checkout main # Now works if origin/main existsIf 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 branchCloning 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 branchOr use git switch (Git 2.23+):
git fetch origin
git switch feature/loginCreate a new branch:
git checkout -b feature/login
# or
git switch -c feature/loginThe -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 -aIf the branch exists on the remote but not locally:
git fetch origin
git merge origin/mainOr create a local tracking branch first:
git checkout main
git checkout - # Go back to previous branch
git merge mainFor 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 yetThis 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 yetFix: 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 mainIf this fails with a detached HEAD error, see Fix: git detached HEAD.
Common Mistake: Using
git checkout --orphanthinking it creates a regular branch. Orphan branches are used for special purposes likegh-pagesor starting a completely separate history. For a normal new branch, usegit 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/mainIf the file is empty or contains garbage, the ref is corrupted.
Fix from the reflog:
git reflogFind the last valid commit hash for the branch and recreate the ref:
git branch -f main abc1234Fix from the remote:
git fetch origin
git branch -f main origin/mainRun git fsck to check for corruption:
git fsck --fullThis 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 mainCheck 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 --recursiveTry a fresh clone. If the local repository is corrupted beyond repair:
cd ..
mv myproject myproject-backup
git clone https://github.com/user/repo.git myprojectThen 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.
Solo developer based in Japan. Every solution is cross-referenced with official documentation and tested before publishing.
Was this article helpful?
Related Articles
Fix: Git "cannot lock ref" – Unable to Create Lock File
How to fix the Git error 'cannot lock ref: Unable to create .git/refs/heads/branch-name.lock' caused by stale lock files, case conflicts, packed-refs corruption, and concurrent operations.
Fix: Git "Your local changes would be overwritten by merge"
How to fix Git error 'Your local changes to the following files would be overwritten by merge' using git stash, commit, checkout, and pull strategies.
Fix: fatal: remote origin already exists
How to fix the 'fatal: remote origin already exists' error in Git by updating the remote URL, removing and re-adding origin, managing multiple remotes, and handling forked repos.
Fix: Git LFS Smudge Filter Error
Resolve Git LFS smudge filter errors by installing Git LFS, fixing credentials, resetting LFS hooks, and handling bandwidth or storage quota issues.