Git
Error
Troubleshooting
Version Control
Master Branch

Fatal: Not a valid object name: 'master' in Git

Codemia Knowledge Hub ·

fatal: Not a valid object name 'master' means the name master does not resolve to any ref or commit in your repository. Most often the repository's default branch is main (or another name) and your command still assumes master. The same logic applies to the 'main' variant of the error.

What Git cannot resolve

Branches, tags and commit hashes all resolve to objects in the repository. If you run:

bash
git branch feature master

and there is no branch, tag or other ref named master, Git prints:

text
fatal: not a valid object name: 'master'

This is not a permissions or network problem. It is about the reference name.

The branch is called main

Many repositories no longer use master as the default branch. Check which branches exist:

bash
git branch -a
text
* feature/login
  main
  remotes/origin/main

Here, use main instead of master.

Other causes

  • the repository has no commits yet
  • you cloned but have not fetched the expected branch
  • the branch exists only on the remote, not locally
  • a script assumes master in every repository

Find the remote default branch

Inspect the remote HEAD reference:

bash
git symbolic-ref refs/remotes/origin/HEAD

A result like this:

text
refs/remotes/origin/main

means the remote default branch is main. You can also fetch and list remote branches:

bash
git fetch origin
git branch -r

That tells you whether the branch does not exist at all or exists remotely but your local refs are out of date.

Use the real branch name

The fix is usually to stop assuming master:

  • use main if that is the real branch name
  • use origin/HEAD when you need the remote default branch dynamically
  • parameterize scripts instead of hardcoding a branch name

For example, instead of:

bash
git diff master..HEAD

use this when the repository uses main:

bash
git diff main..HEAD

Create a local tracking branch

If the branch exists on the remote but not locally:

bash
git fetch origin
git switch -c main --track origin/main

Local commands referring to main will then work. If a script expects master, update the script rather than creating a misleading compatibility branch, unless you have a specific reason.

Empty repository with no commits

In a freshly initialized repository there is no branch tip yet, so no branch name resolves to a commit, and even git branch can be misleading. Create the first commit on the branch you want:

bash
1git init
2git switch -c main
3echo "# demo" > README.md
4git add README.md
5git commit -m "Initial commit"

Habits that cause this error

  • Assuming every repository has a branch named master.
  • Checking only local branches when the branch may exist remotely under another name or not be fetched yet.
  • Hardcoding master in scripts, CI jobs and documentation.
  • Creating extra branches to silence the error instead of referencing the correct existing branch.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions