TechEarl

Git: 'src refspec main does not match any' - How to Fix It

The 'src refspec main does not match any' error almost always means you have not committed yet, or you are pushing a branch name that does not exist. Here is the fix, plus the related 'failed to push some refs (fetch first)' rejection.

Ishan KarunaratnePublished ⏱️ 13 min readUpdated
Share thisCopied
Fixing the Git error src refspec main does not match any when pushing a new branch

src refspec main does not match any means the local source name main did not resolve to a ref Git could push. Common causes are a repository with no commit yet, a different branch name such as master, or a typo. Diagnose the local ref before creating a commit or renaming a branch.

The same reasoning applies to src refspec master does not match any. The later message failed to push some refs is a summary shared by several errors; read the line immediately before it. If that line says rejected ... (fetch first) instead, the remote is ahead of you, and the fix is in failed to push some refs further down.

Here is the full error you are most likely staring at:

text
error: src refspec main does not match any
error: failed to push some refs to 'github.com:you/your-repo.git'

If your default branch is master, the first line reads error: src refspec master does not match any instead. Same problem, same fixes below.

The one-line fix

There is no single command that safely fixes every cause. Start with these read-only checks:

bash
git status
git branch --show-current
git rev-parse --verify HEAD
ResultNext step
Status says no commits yet; HEAD verification failsReview and commit the intended files first
Current branch is master, but you pushed mainPush master, or deliberately rename the branch
Branch command is blank and HEAD resolvesYou are detached; create a new named branch if you want to keep this work
Branch and HEAD are presentCheck the exact source name in the failed push command

After a successful commit on a named branch, git push -u origin HEAD pushes that current branch to the same branch name on origin. Confirm the remote with git remote -v first. An authentication or non-fast-forward rejection after this is a different problem; do not add --force to solve a missing local ref.

What "refspec" actually means

A refspec is just Git's word for "which reference am I pushing, and where does it go." In git push origin main, the main part is the source refspec: the local branch you want to send. When Git says it "does not match any," it is telling you that main does not resolve to anything on your side. This message identifies a local source-resolution problem; it does not prove that remote access or permissions are correct.

This is the whole reason a brand-new repo trips people up. You can git init and git remote add origin ... all you like, but until your first git commit, your repository has no branches at all.

Cause 1: you have not committed yet

This is the common case. Check what Git sees:

bash
git status

If you get something like No commits yet and a list of untracked files, that confirms it. Staging files is not the same as committing them; the branch only comes into being on the commit. (If staging versus committing is fuzzy, I wrote a walkthrough of the Git staging area that clears it up.)

Stage and commit, then push:

bash
git add README.md                 # replace with the files you intend to commit
git diff --cached
git commit -m "Initial commit"
git push -u origin HEAD

The -u sets the current named branch to track its matching remote branch so future pushes are just git push. If you skip it, you may then hit a different message about no upstream branch, which is its own quick fix.

A gotcha worth flagging: if git commit itself fails with Please tell me who you are, the commit never happened, so the branch was never created, and your push will throw the refspec error. Set your identity first (I cover that in fixing the 'Please tell me who you are' error) and commit again.

Cause 2: you are on master, not main

Git versions from 2.28 (July 2020) onward let you configure the default branch name, and most people now use main. But older Git, and many existing repos, still default to master. If you committed on master and then ran git push origin main, Git correctly reports that main does not match any local branch.

Check which branch you are actually on:

bash
git branch

The branch with the * next to it is your current branch. If it says master, you have two choices.

Push the branch you have:

bash
git push -u origin master

Or rename your local branch to main, then push:

bash
git branch -m main
git push -u origin main

git branch -m main renames the current branch to main (the lower-case -m refuses to overwrite an existing branch of that name). This is exactly the snippet GitHub shows you on a fresh empty repository, which is why pasting it on a repo with no commits still fails: rename does not create a branch out of nothing, so commit first.

If you want a clearer mental model of how branches come and go, Git branching explained for beginners is the cluster article for that.

Cause 3: a typo in the branch name

Less common but easy to miss. If your branch is feature/login and you type git push origin feature/loginn, same error. Git is matching the literal name. List your branches and copy the name exactly:

bash
git branch
git push -u origin feature/login

Cause 4: detached HEAD, so no branch was created

If you checked out a commit, tag, or remote ref directly (for example git checkout <sha> or git checkout v1.0), you land in detached HEAD: HEAD is not attached to a branch. Detached HEAD alone does not cause this error: git push origin main still works if a local main exists. It fails here only when that named source is missing. Confirm with:

bash
git status

A detached HEAD reports HEAD detached at <sha>. Create a branch at your current position, then push it:

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

git switch -c recovered-work makes a recovered-work branch pointing at the commit you are sitting on and moves HEAD onto it, so the refspec resolves.

Cause 5: pushing a tag or ref that does not exist

The same error appears when you push a tag or ref name that Git cannot resolve. git push origin v2.0 fails with src refspec v2.0 does not match any if no tag named v2.0 exists yet. Create the tag first, then push it:

bash
git tag v2.0
git push origin v2.0

To push every local tag in one go, use git push origin --tags. The rule is the same as for branches: the name on the right of git push origin <name> has to resolve to something that actually exists locally.

How to tell which cause you have

What git status / git branch showsWhat it meansFix
No commits yetNothing committed; no branch existsStage intended files, inspect the staged diff, commit, then push
Current branch is masterYou are pushing the wrong namePush master, or git branch -m main then push
Current branch is main but commits existLikely a typo in the push commandRe-run with the exact branch name from git branch
HEAD detached at <sha>Current checkout is not attached to a branchCreate a new named branch, then push it
Pushing a tag/ref that does not existThe named tag was never createdgit tag <name> (or --tags) then push
Please tell me who you are on commitCommit failed, so no branch was madeSet user.name / user.email, commit, push

A clean first-push sequence

If you are setting up a new repo from scratch and want to avoid this error entirely, use this order after choosing an empty remote repository and reviewing which files belong in version control:

bash
git init -b main
git add README.md                 # replace with intended project files
git diff --cached
git commit -m "Initial commit"
git remote add origin git@github.com:you/your-repo.git
git push -u origin main

The key is that the commit comes before the push, and the branch name in the last line matches the one you initialized. For the full walkthrough of setting up Git for a new project, including the remote and the first push end to end, that is the canonical guide. If instead you are wiring Git into a folder that already has files in it, see adding Git to an existing project for the right way to run git init over existing work. And if you are starting from a repo that already lives on GitHub, you would clone it rather than init, which I cover in starting work on an existing Git project.

Using SSH like the example above? If the push then complains about Permission denied (publickey), your key is not set up; see adding an SSH key to GitHub. On HTTPS remotes you may instead hit the password authentication removed error, which is a separate auth fix and not related to the refspec. If you are not sure which protocol to use in the first place, the difference between SSH and HTTPS remotes lays out the trade-offs.

Fix "failed to push some refs" (rejected, fetch first)

The refspec error is about the local side having nothing to send. The other common way to end up with failed to push some refs is the opposite problem: your branch exists, but the remote has commits you do not have locally. Read the line above the summary. This version looks like this:

text
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:you/your-repo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use 'git pull'
hint: before pushing again.

(fetch first) and "remote contains work that you do not have locally" are the tells. Nothing is broken; Git is protecting commits that are on the server but not in your copy.

Why the push is rejected (non-fast-forward)

A push only succeeds as a fast-forward: the remote branch tip must be a direct ancestor of the commit you push, so Git can slide the pointer forward without losing anything. The classic sequence that breaks that:

  1. You and a teammate both start from commit A on main.
  2. Your teammate commits B and pushes it. The remote main is now at B.
  3. You commit C locally. Your main is at C, whose parent is A, not B.
  4. You push. There is no straight line from B to C, so the push is rejected as non-fast-forward.

It also happens to one person across two machines, or after someone rewrote remote history with a force push. Short-lived feature branches instead of everyone committing straight to a shared main cut down how often two people collide on the same branch tip.

The fix: pull, then push

Bring the remote commits into your branch first. I almost always use --rebase, which replays your local commits on top of the remote tip and keeps history linear:

bash
git pull --rebase
git push

If both sides touched the same lines, the rebase pauses on a conflict. Resolve it, mark the file resolved, and continue:

bash
git add path/to/resolved-file
git rebase --continue

A merge works too: git pull --no-rebase merges the remote commits in, creating a merge commit when the histories have diverged instead of a straight line. Plain git pull with no setting is fast-forward-only on current Git, so on diverged history it stops rather than picking merge or rebase for you. If git pull complains that it does not know how to reconcile divergent branches, that is a one-time setting for merge or rebase. Do not reach for git reset to silence the rejection: it does not address the cause and can throw away your local commit.

When is --force acceptable?

Forcing overwrites the remote branch with yours, discarding the remote commits Git just warned you about. On a shared branch that is how you delete a teammate's work, so do not force-push a shared branch. The one legitimate case is when you rewrote your own commits (an interactive rebase to squash, an amend, a fixup) on a branch only you use, such as a pull request branch. Then use the lease variant:

bash
git push --force-with-lease

--force-with-lease refuses the push if the remote branch no longer points where your remote-tracking ref says it does, so it will not silently clobber a commit someone pushed while you were rebasing. Plain --force skips that check. One caveat from the Git manual: the lease only knows what your last fetch recorded, so an editor or cron job that runs git fetch in the background can update that ref and quietly defeat the check. When that matters, pin the expected tip explicitly, for example git push --force-with-lease=main:<sha-you-reviewed>, or add --force-if-includes.

ApproachWhat it doesWhen to use
git pull --rebase then git pushReplays your commits on top of remote, linear historyThe default for diverged history you did not intend to rewrite
git pull --no-rebase then git pushMerges remote into local, adds a merge commit when histories divergedSame goal, when you prefer merge semantics
git push --force-with-leaseOverwrites remote, but only if it still matches your last-fetched remote-tracking refYou rewrote your own commits on a branch that is yours
git push --forceOverwrites remote unconditionallyAlmost never; it can erase work you did not check for

If you protect main (see protecting your main branch), force pushes to it are blocked at the server anyway.

Other causes behind the same summary line

If you are new to all of this, the Git for beginners guide is the hub that ties the staging, branching, and pushing pieces together in order.

FAQ

Because there is no local branch named main to push. The most common reason is that you have not made any commits yet, so no branch exists. Run git status to confirm, then stage and commit before pushing.

Run git branch to see your actual branch name. You are probably on master rather than main. Either push master, or rename your branch with git branch -m main and push that instead.

No. Staging is not committing. A branch only comes into existence when a commit points at it, so you must run git commit, not just git add, before the branch and the push will work.

Neither. The src refspec error is entirely local. Git is telling you the branch you named does not resolve to anything on your machine, so it never even tries to contact the remote for that ref.

Yes. The message is identical apart from the branch name. Whether it says main or master, the cause is the same: that branch does not resolve to anything locally, usually because you have not committed yet. Commit first, then push the branch you are actually on (check it with git branch).

Both fix the fetch-first rejection. Rebase replays your commits on top of the remote and keeps history linear; plain pull merges and adds a merge commit. Either way your local commits are kept. You may have to resolve a conflict if the same lines changed on both sides, but nothing is discarded.

On a branch other people use, no. Force pushing overwrites the remote and can delete a teammate's commits. Only force when you rewrote your own commits on your own branch, and use --force-with-lease so it refuses if the remote moved.

Sources

Authoritative references this article was fact-checked against.

Tagssrc refspec main does not match anyGitVersion Controlgit pushGit errorsfailed to push some refsnon-fast-forward

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Systems and Network Architect · Chief Technology Officer

Systems and network architect and Chief Technology Officer with more than two decades designing, building, and running production software, cloud and network architecture, Linux systems, and the bare metal underneath them, and lately working AI into the stack. A US Army veteran who served in Operation Iraqi Freedom. What I write here is drawn from the full arc of that work, across architecture, engineering, and operations, not any single job.

Keep reading

Related posts