Fix "src refspec main does not match any"

한국어로 읽기 →

Cause: Git cannot find local history named main to push. This can happen before the first commit or when the branch name differs from the name in your push command.

Fix: If your current branch is main and status shows No commits yet, create the first commit. Stage the file, then review everything that will enter that commit.

git add readme.md
git diff --cached --stat
git diff --cached

Stop here if another file or an unintended change appears. Adding readme.md leaves previously staged files in place. Run the following commands one at a time only when the complete file list and changes match the intended commit.

git commit -m "add readme"
git push -u origin main

If commits already exist, compare branch names in the next section instead of repeating the first-commit steps. Do not continue to the next command after a failure.

These screenshots show the actual commands and results. Select an image to open it at full size. On small screens, scroll sideways inside a wide image.

1. Check whether the first commit is missing

Run these commands in the repository terminal. git status shows the working files and commit state; the second command prints the current branch name.

git status
git branch --show-current

In this example, the branch name is main and status shows No commits yet. The branch has a name, but there is no first commit yet. Running git push origin main cannot find history to send, so it reports src refspec main does not match any. A refspec identifies the local history to send and its remote destination.

Choose the next step from your actual output.

Check whether the first commit is missing: comparison
Observed stateNext action
Current name is main and status shows No commits yetReview all intended files, then create the first commit below.
Commits exist and the current name is master or another nameCompare that name with main in the push command. Confirm the team's branch naming and intended destination first.
main already has commitsDo not create another first commit. Check the exact push command and the local name it specifies.

origin names the remote repository; main identifies the local branch sent by this command. A different current name is not a reason to blindly create a new main branch or commit in a different project. First confirm that you are inspecting the intended repository.

The push fails with src refspec main does not match any.
Look for: The push fails with src refspec main does not match any.

Expected resultThe branch is main, but it shows No commits yet and the src refspec error.

2. Create the first commit from a reviewed file

Follow this step only for main with no first commit. Open readme.md, inspect what you intend to upload, and check the remote address.

git remote -v

The address for origin must be the intended repository. If it is missing or different, stop before sending anything and check the project's remote configuration.

Now add the file to the staging area, which holds changes selected for the next commit.

git add readme.md
git diff --cached --stat
git diff --cached

--stat shows the file summary and change counts. git diff --cached without that option shows the actual staged changes. Read the contents as well as the file names. This example's intended first commit contains only readme.md.

If another file such as notes.txt appears, stop before commit and push. git add readme.md does not clear the staging area. A normal git commit records all staged changes, so earlier work can be included. Do not unstage or delete another file as part of these instructions. First agree with the person responsible for that work on what belongs in this commit.

When the list contains only readme.md and its contents are correct, run these commands one at a time.

git commit -m "add readme"
git push -u origin main

commit records the reviewed changes; push sends that history to the remote. -u also sets the tracking relationship for future use. If commit reports an error such as missing author configuration, resolve it before pushing. If push reports an authentication, permission, or non-fast-forward error, resolve that error before continuing.

The first commit is created and main is pushed to the remote.
Look for: The first commit is created and main is pushed to the remote.

Expected resultThe first commit is created and main is pushed to the remote.

3. Check the remote and tracking branch

After the first commit and push succeed, inspect the committed files and tracking state.

git show --format= --name-only HEAD
git status -sb
git push

The first command lists the files included in the commit just created. This example should list only readme.md. git status -sb shows main...origin/main, and the next push reports Everything up-to-date, meaning the same history has already been sent.

If the first commit includes an unintended file, stop additional pushes and inspect the commit and whether it reached the remote. Do not overwrite shared history with a force push. The full staged review earlier in this guide is intended to catch this before committing.

For a project that already had commits, first align the actual local branch name with the intended name to push. A force push does not create missing local history.

main tracks origin/main, and the local and remote main branches have matching commits.
Look for: main tracks origin/main, and the local and remote main branches have matching commits.

Expected resultmain tracks origin/main, and the local and remote main branches have matching commits.

References

Another Git problem?

Fix "The current branch main has no upstream branch" →

Rename a Git branch: A local branch before pushing →

Fix "fatal: not a git repository" by locating the repository in PowerShell →