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:
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:
git status
git branch --show-current
git rev-parse --verify HEAD| Result | Next step |
|---|---|
| Status says no commits yet; HEAD verification fails | Review and commit the intended files first |
Current branch is master, but you pushed main | Push master, or deliberately rename the branch |
| Branch command is blank and HEAD resolves | You are detached; create a new named branch if you want to keep this work |
| Branch and HEAD are present | Check 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:
git statusIf 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:
git add README.md # replace with the files you intend to commit
git diff --cached
git commit -m "Initial commit"
git push -u origin HEADThe -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:
git branchThe branch with the * next to it is your current branch. If it says master, you have two choices.
Push the branch you have:
git push -u origin masterOr rename your local branch to main, then push:
git branch -m main
git push -u origin maingit 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:
git branch
git push -u origin feature/loginCause 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:
git statusA detached HEAD reports HEAD detached at <sha>. Create a branch at your current position, then push it:
git switch -c recovered-work
git push -u origin recovered-workgit 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:
git tag v2.0
git push origin v2.0To 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 shows | What it means | Fix |
|---|---|---|
No commits yet | Nothing committed; no branch exists | Stage intended files, inspect the staged diff, commit, then push |
Current branch is master | You are pushing the wrong name | Push master, or git branch -m main then push |
Current branch is main but commits exist | Likely a typo in the push command | Re-run with the exact branch name from git branch |
HEAD detached at <sha> | Current checkout is not attached to a branch | Create a new named branch, then push it |
| Pushing a tag/ref that does not exist | The named tag was never created | git tag <name> (or --tags) then push |
Please tell me who you are on commit | Commit failed, so no branch was made | Set 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:
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 mainThe 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:
! [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:
- You and a teammate both start from commit
Aonmain. - Your teammate commits
Band pushes it. The remotemainis now atB. - You commit
Clocally. Yourmainis atC, whose parent isA, notB. - You push. There is no straight line from
BtoC, 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:
git pull --rebase
git pushIf both sides touched the same lines, the rebase pauses on a conflict. Resolve it, mark the file resolved, and continue:
git add path/to/resolved-file
git rebase --continueA 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:
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.
| Approach | What it does | When to use |
|---|---|---|
git pull --rebase then git push | Replays your commits on top of remote, linear history | The default for diverged history you did not intend to rewrite |
git pull --no-rebase then git push | Merges remote into local, adds a merge commit when histories diverged | Same goal, when you prefer merge semantics |
git push --force-with-lease | Overwrites remote, but only if it still matches your last-fetched remote-tracking ref | You rewrote your own commits on a branch that is yours |
git push --force | Overwrites remote unconditionally | Almost 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
- No upstream set yet, so Git does not know where to push: see setting an upstream branch.
- A server-side hook rejected the push (a CI check, a commit-message rule): see pre-receive hook declined.
- A commit contains a file over GitHub's size limit: see the file size limit error.
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
Sources
Authoritative references this article was fact-checked against.
- git-push - Official Git documentationgit-scm.com
- Pro Git - Recording Changes to the Repositorygit-scm.com
- Create a repository - GitHub Docsdocs.github.com
- Pro Git - Git Branching: Rebasinggit-scm.com
- Dealing with non-fast-forward errors - GitHub Docsdocs.github.com
- git-pull - Official Git documentationgit-scm.com





