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:
and there is no branch, tag or other ref named master, Git prints:
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:
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
masterin every repository
Find the remote default branch
Inspect the remote HEAD reference:
A result like this:
means the remote default branch is main. You can also fetch and list remote branches:
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
mainif that is the real branch name - use
origin/HEADwhen you need the remote default branch dynamically - parameterize scripts instead of hardcoding a branch name
For example, instead of:
use this when the repository uses main:
Create a local tracking branch
If the branch exists on the remote but not locally:
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:
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
masterin scripts, CI jobs and documentation. - Creating extra branches to silence the error instead of referencing the correct existing branch.
Related reading
- fatal The current branch master has no upstream branch
- fatal unable to access 'https//gitlab-ci-tokenMASKEDgitlab.mydomain.com/xxx.git/' SSL certificate problem unable to get issuer certificate
- Fetch from origin with deleted remote branches
- fetch in git doesn't get all branches
- FBSOpenApplicationErrorDomain code 4 error
- Fetch request failing in Node.js Client network socket disconnected before secure TLS connection was established
- Filename too long in Git for Windows
- Find a commit on GitHub given the commit hash
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.