Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~4 min readGit
#Git#Branches#Remotes#CLI
A Git branch crate missing its ref label at a cyan push gate, an amber commit card waiting beside it

Introduction & Quick Summary

text
$ 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:

bash
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 to main, but git init on older Git or with init.defaultBranch=master still creates master.
  • Detached HEAD. You are on a commit, not a branch, so main does not exist.
  • Typo or case. Main, mian, or a tag name mistaken for a branch.
What git status showsMeaningFix
No commits yet on mainUnborn branchgit commit first
On branch masterBranch name differsgit push -u origin master or rename
HEAD detached at abc1234Not on a branchgit switch -c main, then push
Nothing to commit, working tree clean, branch mainPushing an empty/other refCheck 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 init writes .git/HEAD pointing at refs/heads/main, but that ref is only created by the first commit. git status says No commits yet.
  • A different branch name. The ref exists, just not under the name you asked for.
  • Detached HEAD. HEAD points directly at a commit; no branch ref tracks it, so pushing a branch name requires creating one.

A faithful reproduction:

bash
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 any
bash
git 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

CommandPushesWorks from detached HEAD
git push origin mainLocal main to mainNo
git push origin HEADCurrent commit to matching branchYes
git push origin HEAD:mainCurrent commit to mainYes
git push -u origin masterLocal masterNo
git push --tagsAll tagsYes
git push origin refs/tags/v1.2.0One tag explicitlyYes
SituationFirst commandThen
New repositorygit add . && git commit -m "init"git push -u origin main
Local branch is mastergit branch -M maingit push -u origin main
Detached HEAD with commitsgit switch -c maingit push -u origin main
Wrong remote defaultgit remote show origingit push -u origin <branch>

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: Nothing has been committed

bash
git init -b main git add . git commit -m "feat: bootstrap" git push -u origin main

Staging 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

bash
git branch -a # * master git push -u origin master # immediate fix

To standardize on main, rename before pushing:

bash
git branch -M main git push -u origin main git remote set-head origin -a # refresh origin/HEAD

git 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.

bash
git fetch origin git rebase origin/main git push -u origin main

Case 3: Detached HEAD

bash
git checkout v1.4.0 # HEAD detached at 9a1c2f7 git push origin main # error: src refspec main does not match any

Commit work made on a detached HEAD before switching, otherwise it becomes unreachable. Create the branch first, then push:

bash
git switch -c hotfix/1.4.1 git push -u origin HEAD

HEAD 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

bash
git push origin v1.2.0

If 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:

bash
git tag -a v1.2.0 -m "release 1.2.0" git push origin refs/tags/v1.2.0

Checking 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:

bash
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

bash
#!/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 main before the first commit. The most common form of this error, and no amount of remote configuration changes it.
  • git init default branch changed in 2.28. init.defaultBranch defaults to master unless configured; git config --global init.defaultBranch main prevents the mismatch on new repositories.
  • Case-sensitive refs. Main and main are different refs on Linux servers; macOS case-insensitive filesystems can hide the mistake locally.
  • git push --all from 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 1 on a tag can leave no local branch; use git switch -c main after fetching, or push HEAD.

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 documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.