Git: src refspec main does not match any on push
The push had no local ref to send: no commit yet, a master branch, a detached HEAD, or a name typo. Check status, branches and log before pushing.

Introduction & Quick Summary
$ git push origin main
error: src refspec main does not match any
error: failed to push some refs to 'git@github.com:acme/api.git'Git has no local ref named main to send, so the push has nothing to offer. The remote is fine and authentication already succeeded — this failure happens before any transfer. Either the branch does not exist locally, has no commits, or is named something else.
Diagnose in three commands:
git status -sb # current branch, ahead/behind, untracked
git branch -a # local and remote branches
git log --oneline -3 # are there commits at all?The usual causes in order of frequency:
- No commit yet. A freshly
git init-ed repository has an unborn HEAD; there is no ref to push. - The branch is
master. GitHub renamed the default tomain, butgit initon older Git or withinit.defaultBranch=masterstill createsmaster. - Detached HEAD. You are on a commit, not a branch, so
maindoes not exist. - Typo or case.
Main,mian, or a tag name mistaken for a branch.
What git status shows | Meaning | Fix |
|---|---|---|
No commits yet on main | Unborn branch | git commit first |
On branch master | Branch name differs | git push -u origin master or rename |
HEAD detached at abc1234 | Not on a branch | git switch -c main, then push |
Nothing to commit, working tree clean, branch main | Pushing an empty/other ref | Check git log and branch list |
Core Mechanics, Signatures & Reproducible Setup
git push <remote> <refspec> takes a source ref and an optional destination. The source is resolved locally before any network call. git push origin main means "take my local main and put it at main on origin". If refs/heads/main does not resolve, Git reports src refspec main does not match any.
Three ref states produce this error even when the repository looks healthy:
- Unborn HEAD.
git initwrites.git/HEADpointing atrefs/heads/main, but that ref is only created by the first commit.git statussaysNo commits yet. - A different branch name. The ref exists, just not under the name you asked for.
- Detached HEAD.
HEADpoints directly at a commit; no branch ref tracks it, so pushing a branch name requires creating one.
A faithful reproduction:
mkdir repo-demo && cd repo-demo
git init -b main
git remote add origin git@github.com:acme/empty.git
git push -u origin main
# error: src refspec main does not match anygit commit --allow-empty -m "chore: initial commit"
git push -u origin main
# branch 'main' set up to track 'origin/main'The remote helps explain the rest of the error class: GitHub, GitLab and Bitbucket create the default branch on their side only when it receives a commit. Pushing to a branch that does not exist remotely is allowed if you have push rights; that is why src refspec failures are local-only errors and never a permissions problem.
Key Comparison Table
| Command | Pushes | Works from detached HEAD |
|---|---|---|
git push origin main | Local main to main | No |
git push origin HEAD | Current commit to matching branch | Yes |
git push origin HEAD:main | Current commit to main | Yes |
git push -u origin master | Local master | No |
git push --tags | All tags | Yes |
git push origin refs/tags/v1.2.0 | One tag explicitly | Yes |
| Situation | First command | Then |
|---|---|---|
| New repository | git add . && git commit -m "init" | git push -u origin main |
Local branch is master | git branch -M main | git push -u origin main |
| Detached HEAD with commits | git switch -c main | git push -u origin main |
| Wrong remote default | git remote show origin | git push -u origin <branch> |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
Case 1: Nothing has been committed
git init -b main
git add .
git commit -m "feat: bootstrap"
git push -u origin mainStaging is not committing. git status listing files under Changes to be committed means the objects exist but no branch does. A repository with a .gitignore and no source files also needs --allow-empty if the first real commit comes later.
Case 2: master versus main
git branch -a
# * master
git push -u origin master # immediate fixTo standardize on main, rename before pushing:
git branch -M main
git push -u origin main
git remote set-head origin -a # refresh origin/HEADgit branch -M renames even if main already exists as a stale local branch, so check git branch first. If the remote already has a main with commits, a plain push may be rejected as non-fast-forward; fetch and rebase instead of forcing.
git fetch origin
git rebase origin/main
git push -u origin mainCase 3: Detached HEAD
git checkout v1.4.0
# HEAD detached at 9a1c2f7
git push origin main
# error: src refspec main does not match anyCommit work made on a detached HEAD before switching, otherwise it becomes unreachable. Create the branch first, then push:
git switch -c hotfix/1.4.1
git push -u origin HEADHEAD as a refspec is the portable form and works regardless of the branch name, which makes it the right choice in scripts and CI checkouts.
Case 4: The refspec is a tag
git push origin v1.2.0If the tag was never created, Git reports src refspec v1.2.0 does not match any. Tags also need an explicit push by default in most setups:
git tag -a v1.2.0 -m "release 1.2.0"
git push origin refs/tags/v1.2.0Checking names is cheaper than guessing: git tag -l 'v1.2*' and git ls-remote --tags origin.
Case 5: Monorepo subdirectory confusion
Running git push inside a subdirectory of a larger repository pushes the parent repository's branch. If that checkout has a different branch checked out, the name in your command may not exist:
git rev-parse --show-toplevel # which repository am I in?
git rev-parse --abbrev-ref HEAD # which branch is checked out?Nested .git directories from vendor drops or copied projects make this common; git rev-parse settles it immediately.
Case 6: Scripts that assume main
#!/usr/bin/env bash
set -euo pipefail
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
[ "$BRANCH" = "HEAD" ] && { echo "detached HEAD"; exit 1; }
git push -u origin "$BRANCH"Hardcoding main in a deployment script breaks on every repository that still uses master or a release/* branch. Resolve the branch at runtime and fail loudly on a detached HEAD.
Common Compilation/Runtime Mistakes & Version Gotchas
git push -u origin mainbefore the first commit. The most common form of this error, and no amount of remote configuration changes it.git initdefault branch changed in 2.28.init.defaultBranchdefaults tomasterunless configured;git config --global init.defaultBranch mainprevents the mismatch on new repositories.- Case-sensitive refs.
Mainandmainare different refs on Linux servers; macOS case-insensitive filesystems can hide the mistake locally. git push --allfrom an unborn branch. Nothing is pushed, and the exit code may look successful because there was simply nothing to do.- Empty directories. Git tracks files, not directories, so a
src/tree with no files produces no commit and no branch. - Shallow or partial clones in CI. A shallow clone with
--depth 1on a tag can leave no local branch; usegit switch -c mainafter fetching, or pushHEAD.
Compare a local branch with its remote counterpart using the diff tool after fetching, and use the regex tester to filter long git log output when you need to find which commit a branch is missing.
Primary references: git-push defines the refspec grammar and the <src>:<dst> form, and Working with Remotes covers tracking branches and the first push.
Official references
primary documentationThe Git documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit: fatal: not a git repository (or any parent): .git
The command ran outside a working tree or the .git metadata is gone. Confirm the root, restore a deleted .git, and repair worktree pointers.
Git: fatal: could not read from remote repository
Git could not authenticate or reach the remote. Check the remote URL, test SSH with -T, verify the key is offered, and use HTTPS on 443 when 22 is blocked.
Git: fatal: Need to specify how to reconcile divergent branches
Git 2.34 and later refuse to guess between merge, rebase and fast-forward. Set pull.rebase or pull.ff once, and know what each strategy does to history.
Git: fatal: unable to auto-detect email address
A commit needs an author identity. Containers and fresh installs have no user.email, so Git guesses from the OS user and refuses to continue.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.