Fix Git’s “src refspec does not match any” error

Check the local source ref, initial commit, detached HEAD, and exact push argument before changing remote configuration.

Best practices
Illustration of Git branches and version history

The error means Git cannot resolve the source you asked it to push. Common causes are a misspelled branch name or a new repository with no first commit. Diagnose the local source separately from authentication and remote-address problems.

Check the branch you actually named

git status
git branch
git show-ref --verify refs/heads/main
git log --oneline -1

The verification command succeeds only if that exact local branch exists. Replace main with the intended name; main, master, and Main are different ref names. Avoid case-only naming differences across filesystems.

# Use the branch that actually exists:
git push origin feature/login

A typo such as featuer/login or a quoted trailing space in "main " changes the source argument. In CI, inspect the value supplied by the script without exposing credentials. An empty or unexpected variable can produce a different push request than intended.

Create a first commit in a new repository

Before the first commit, Git can display a current branch name even though no branch ref points to a commit yet:

git status
# Review ignore rules and add only intended files.
git add -- README.md
git commit -m "Initial commit"
git branch --show-current
git push -u origin main

Use the branch name printed by the preceding command. Do not assume git init created main on every machine.

Preserve detached work on a branch

Detached HEAD is not itself an invalid source: HEAD can still identify a commit. Creating a branch often makes the intent clearer:

git switch -c recovered-work
git push -u origin recovered-work

An explicit git push origin HEAD:refs/heads/recovered-work is another way to publish the current commit, but does not create a local branch for you. If a rebase or another operation is in progress, resolve that operation deliberately before treating the checkout as an ordinary detached session.

Verify the destination separately

git remote -v
git tag --list

A missing remote, incorrect URL, or authentication failure usually produces a different diagnostic. Fixing the remote does not create a missing local branch. Tags can also be sources: confirm the tag exists, then push the specific tag with git push origin refs/tags/v1.0. --tags publishes all tags and is unnecessary when only one was intended.

If both a branch and tag have the same short name, use a fully qualified ref to avoid ambiguity. A later “failed to push some refs” line is a summary; the preceding specific error is the useful starting point.

See Git push refspecs and git-show-ref.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting