Why git push says "src refspec main does not match any", how to tell which of the two usual causes you have, and the exact commands to fix each.
Git prints error: src refspec main does not match any when you run git push origin main and your local repository has no branch called main that points at a commit. Two causes cover almost every case. Either the repository has no commits yet, so main is only a name with nothing behind it, or your local branch is called something else, usually master. Run git status: "No commits yet" means commit first; "On branch master" means rename the branch or push it under the remote's name. The sections below show both fixes, plus the error people tend to hit right after.
What the message actually means
In git push origin main, the word main is a refspec. Git's documentation describes the format as <src>:<dst>: the source is the local branch (or commit) to send, the destination is the branch to update on the remote. When you leave out the :<dst> part, Git uses the same name on both sides, so main means "send my local main to the remote's main".
"src refspec main does not match any" is Git saying the source half failed: it looked for something local called main and found nothing it could push. The problem is in your local repository, not on GitHub, GitLab or Bitbucket.
Find your case in ten seconds
Run these two commands in the repository:
git status
git branch --show-current- What you see — Cause — Fix
- git status says "No commits yet" — The branch has no commits — Add files, commit, push
- git branch --show-current prints master — Local branch has a different name — Rename to main, or push master to main
- It prints another name, such as dev or a misspelling — You typed a name that does not exist locally — Push the branch you are actually on
One trap: in a brand-new repository, git branch on its own prints nothing at all, even though git status says "On branch main". A branch only shows up in that list once it points at a commit. If you used git branch to check and saw an empty list, the repository has no commits.
Case 1: the repository has no commits yet
This is the classic first-push mistake: git init, git remote add origin …, then git push -u origin main before anything was committed. Here is the full sequence in a fresh repository (output from Git 2.39.3; your remote URL will differ):
$ git init -b main
$ git remote add origin <your-repo-url>
$ git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to '<your-repo-url>'
$ git status
On branch main
No commits yetThe fix is to make the first commit:
git add .
git commit -m "First commit"
git push -u origin mainIf the commit step prints "nothing to commit (create/copy files and use "git add" to track)", the commit did not happen and the push will fail again with the same error. That happens when the folder is empty or when every file in it is excluded by .gitignore. Add at least one tracked file, such as a README, and commit again.
The -u flag (--set-upstream) records origin/main as the branch your local main tracks, so later you can type git push and git pull without arguments.
Case 2: your local branch is called master
Git and GitHub disagree on the default name. Git's own documentation still says the built-in default for git init is master, and that it will change to main when Git 3.0 is released. GitHub switched the default branch for new repositories to main on October 1, 2020. So if your machine has no setting telling Git otherwise, git init creates master, while GitHub's own instructions for pushing a local project use main. The result is this error, even though you committed everything.
You have two ways out. Rename the local branch to match the remote:
git branch -m master main
git push -u origin main-m renames the branch along with its config and reflog. If a local main already exists and you want to overwrite it, -M forces the rename.
Or keep the local name and tell Git where to send it:
git push -u origin master:mainThat pushes your local master into the remote's main. git push origin HEAD:main does the same for whatever branch you are on; Git's documentation calls this form convenient when you do not want to think about the local name. Note that it does not help in Case 1: with no commits, Git replies "src refspec HEAD does not match any".
To stop this happening on the next project, set the default once (needs Git 2.28 or later, the release that added the setting):
git config --global init.defaultBranch mainThe setting only affects repositories you create from now on. Existing branches are not renamed.
Case 3: the name is wrong for another reason
If git branch --show-current prints dev, feature/login or anything other than what you typed, push the branch you are on: git push -u origin dev, or git push -u origin HEAD to push the current branch under its own name. Branch names must match exactly, so check spelling and capitalisation against the output of git branch.
The error you may hit next: rejected, then unrelated histories
If you created the remote repository with a README, licence or .gitignore already in it, fixing the refspec often leads straight to a second failure. The remote main now has a commit your local repository has never seen:
$ git push -u origin main
! [rejected] main -> main (fetch first)
error: failed to push some refs to '<your-repo-url>'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally.The usual advice is to pull first. Depending on your Git version and pull settings, Git may first ask you to choose how to reconcile divergent branches (git pull --no-rebase origin main chooses a merge). That merge then fails with:
$ git pull --no-rebase origin main
fatal: refusing to merge unrelated historiesBoth repositories started separately, so they share no common commit, and Git refuses to merge them by default. The git-merge documentation describes --allow-unrelated-histories as overriding that safety check for projects that "started their lives independently", which is exactly what happened here:
git pull --no-rebase origin main --allow-unrelated-histories
git push -u origin mainGitHub's guide to adding locally hosted code tells you not to initialise the new repository with a README, licence or gitignore file. You can add them after the first push.
If this happens in a take-home or a live coding round
If a take-home is handed in as a repository link, do not leave the first push until just before the deadline. That is the worst time to discover the branch is called master. Make the first push as soon as you start the assignment, then check that it landed: open the repository page in a browser, or run git ls-remote origin, which lists the branches and commits that actually exist on the remote.
If the error appears while someone is watching, in a pairing session or a screen-shared round, it is a chance to show how you debug rather than a mark against you. Say what the message means ("Git can't find a local branch called main"), run git status, name the cause you see and fix it. Interviewers watching a live round see your process either way, so a short, calm diagnosis is the best outcome available.
If you are preparing for live coding rounds more broadly, Verve AI's Coding Copilot reads a problem from your screen and returns a solution in 21+ programming languages. For another Git error that tends to show up at the worst moment, see our guide to the RPC failed HTTP 408 error on push, and for language-specific practice, our Java coding interview questions.
Sources
- Refspec format <src>:<dst>, omitted destination uses the same name, HEAD:main form, -u/--set-upstream: https://git-scm.com/docs/git-push
- git init default branch is master, changing to main with Git 3.0; -b/--initial-branch and init.defaultBranch: https://git-scm.com/docs/git-init
- init.defaultBranch added in Git 2.28; only affects new repositories: https://github.blog/open-source/git/highlights-from-git-2-28/
- GitHub default branch for new repositories became main on October 1, 2020: https://github.blog/changelog/2020-10-01-the-default-branch-for-newly-created-repositories-is-now-main/
- New GitHub repositories default to main; existing repositories not affected: https://github.com/github/renaming
- git branch -m / -M rename behaviour, --show-current, listing branches: https://git-scm.com/docs/git-branch
- GitHub's steps for pushing a local project (git init -b main, then pushing to main) and the advice not to initialise the remote with a README, licence or gitignore: https://docs.github.com/en/migrations/importing-source-code/using-the-command-line-to-import-source-code/adding-locally-hosted-code-to-github
- Rejected pushes when the remote has commits you do not have; pull or fetch and merge: https://docs.github.com/en/get-started/using-git/dealing-with-non-fast-forward-errors
- --allow-unrelated-histories overrides the default refusal to merge histories with no common ancestor: https://git-scm.com/docs/git-merge
- git ls-remote lists references in a remote repository: https://git-scm.com/docs/git-ls-remote
- Terminal output (Case 1, HEAD:main on an empty repository, "nothing to commit", rejected push): reproduced in scratch repositories with Git 2.39.3 (Apple Git-146) on macOS. The unrelated-histories error appears after choosing a merge strategy (--no-rebase).
- Coding Copilot reads the question from the screen; 21+ output languages: facts.md, https://docs.vervecopilot.com/features/coding-copilot
Ryan
CEO of Verve AI








