3273
votes

I clone my repository with:

git clone ssh://xxxxx/xx.git 

But after I change some files and add and commit them, I want to push them to the server:

git add xxx.php
git commit -m "TEST"
git push origin master

But the error I get back is:

error: src refspec master does not match any.  
error: failed to push some refs to 'ssh://xxxxx.com/project.git'
30
@Marco That's not a duplicate. That one is a very specific issue about pushing a local branch to a remote branch. This one is about initializing a repo and pushing it up. They produce the same error, but the REASONS they produce that error and the fixes are entirely different. Also, sinoohe, you should accept an answer. Probably the first one, seeing as it answers the question and has helped over 350 people.tandrewnichols
Hope this post would be useful to somebody- samranga.blogspot.com/2015/07/… The error in the question can be popped even when tried to Create a git BitBucket repository from an already locally existing projectSamitha Chathuranga
Yet another simple task made difficult by Git. The Git devs should use Stack Overflow as feedback in their SDLC loop. 850,000+ people should indicate something is seriously wrong with Git's workflow. They need to hire a UX expert because they clearly cannot git it right on their own.jww
If you didnt add git add with dot or some files this error also will appear.Blasanka
Recently Github/Git does not have a default "master" branch. "master" has been changed to "main" branch. So this may be a possible reason for this error.Harini Sj

30 Answers

4787
votes

Maybe you just need to commit. I ran into this when I did:

mkdir repo && cd repo
git remote add origin /path/to/origin.git
git add .

Oops! Never committed!

git push -u origin master
error: src refspec master does not match any.

All I had to do was:

git commit -m "initial commit"
git push origin master

Success!

1325
votes
  1. Try git show-ref to see what refs you have. Is there a refs/heads/master?

Due to the recent "Replacing master with main in GitHub" action, you may notice that there is a refs/heads/main. As a result, the following command may change from git push origin HEAD:master to git push origin HEAD:main

  1. You can try git push origin HEAD:master as a more local-reference-independent solution. This explicitly states that you want to push the local ref HEAD to the remote ref master (see the git-push refspec documentation).
275
votes
  1. My changes were already committed
  2. Force push still gave me the same error.

So I tried Vi's solution:

git push origin HEAD:<remoteBranch> 

This worked for me.

251
votes

I also had a similar error after deleting all files on my local computer, and I have to clean up all files in the repository.

My error message was something like this:

error: src refspec master does not match any.
error: failed to push some refs to 'git@github ... .git'

And it was solved by executing the following commands:

touch README
git add README

git add (all other files)
git commit -m 'reinitialized files'
git push origin master --force  # <- caution, --force can delete others work.
134
votes
git push -u origin master
error: src refspec master does not match any.

For that you need to enter the commit message as follows and then push the code:

git commit -m "initial commit"

git push origin master

Successfully pushed to master.

128
votes

For me I had to make sure the public key is properly configured on the server (appended in ~/.ssh/authorized_keys) and in GitHub/Bitbucket (added to my SSH keys on GitHub or Bitbucket) - they need to match. Then:

git add --all :/
git commit -am 'message'
git push -u origin master
99
votes

I found this happened in a brand new repository after I Git added only a directory.

As soon as I added a file (e.g. a README), Git push worked great.

72
votes

Missing or skipping git add . or git commit may cause this error:

git push -u origin master
Username for 'https://github.com': yourusername
Password for 'https://[email protected]': 
error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/yourusername/foobar.git'

To fix it, reinitialize and follow the proper sequence:

git init
git add .
git commit -m 'message'
git *create remote
git push -u origin master
68
votes

To fix it, re-initialize and follow the proper code sequence:

git init
git add .
git commit -m 'message'
git push -u origin master
56
votes

Make sure you've added first, and then commit/ push:

Like:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master
56
votes

I faced the same problem, and I used --allow-empty:

$ git commit -m "initial commit" --allow-empty
...
$ git push
...

Supplement

One of main reasons of this problem is that some Git servers, such as BitBucket, don't have their master branch initialized when a fresh repository is cloned.

51
votes

This happens too when you are in a specific branch and try to push another branch that does not exist yet, like:

$ git branch
* version-x  # you are in this branch
  version-y

$ git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'origin_address'
41
votes

For me,following worked to move untracked files:

git add --all

Next, I followed similar steps

 git commit -m "First commit"

Then,

git remote add origin git@github.....

Last but not the least:

git push -u origin master

As you do this, Windows security will pop up asking for your username and password.

40
votes

I faced the same issue some days ago.

If you created a new repository nowadays(2020) then the default branch is main on GitHub.

you can check on GitHub now in your repository branches.

and you can also check branch on the terminal by running the command:

git branch

so that's why you need to run

git push origin main

instead of

git push origin master

Goodluck

29
votes

In my case, I forgot to include the .gitignore file. Here are all the steps required:

  1. Create an empty Git repository on remote,
  2. On local create the .gitignore file for your project. GitHub gives you a list of examples here
  3. Launch a terminal, and in your project do the following commands:

    git remote add origin YOUR/ORIGIN.git
    
    git add .
    
    git commit -m "initial commit or whatever message for first commit"
    
    git push -u origin master
    
27
votes

Just add an initial commit. Follow these steps:

  • git add .

  • git commit -m "initial commit"

  • git push origin master

This worked for me.

27
votes

My issue was that the 'master' branch hadn't been created locally yet.

A quick

git checkout -b "master"

created the master branch, at which point, a quick

git push -u origin master

pushed the work up to the Git repository.

26
votes

After the GitHub update 01.10.20 you should use main instead of master.

Do it like these way...

  1. Create a repository on GitHub
  2. Delete existing .git file on your local directory
  3. Go to local project directory and type git init
  4. git add .
  5. git commit -m"My First Commmit"
  6. Now check your branch name it will be master in your local project
  7. git remote add origin <remote repository URL past here from the github repository> then type git remote -v
  8. git push -f origin master
  9. Now check the github repository you will see two branch 1. main 2. master
  10. In your local repository create new branch and the branch name will be main
  11. git checkout main
  12. git merge master
  13. git pull origin main
  14. git push -f origin main

Note: from 01.10.20 github decided use main instead of master branch use default branch name

22
votes

You probably forgot the command "git add ." after the "git init" command.

20
votes

This just mean you forgot to do the initial commit, try

git add .
git commit -m 'initial commit'
git push origin master
20
votes

I also followed GitHub's directions as follows below, but I still faced this same error as mentioned by the OP:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master

For me, and I hope this helps some, I was pushing a large file (1.58 GB on disk) on my MacOS. While copy pasting the suggested line of codes above, I was not waiting for my processor to actually finish the add . process. So When I typed git commit -m "message" it basically did not reference any files and has not completed whatever it needs to do to successfully commit my code to GitHub.

The proof of this is when I typed git status usually I get green fonts for the files added. But everything was red. As if it was not added at all.

So I redid the steps. I typed git add . and waited for the files to finish being added. Then I followed through the next steps.

20
votes

It happens if you forget to commit before pushing for the first time. Just run:

git commit -m "first commit"
19
votes
  1. First, git add .
  2. Second, git commit -m "message"
  3. Third, git push origin branch

Please check for spelling mistakes because that could also give that error.

19
votes

To check the current status, git status.

And follow these steps as well:

git init
git add .
git commit -m "message"
git remote add origin "github.com/your_repo.git"
git push -u origin master
18
votes

I had the same problem when I missed to run:

git add .

(You must have at least one file, or you will get the error again.)

17
votes

This happens when you have added your file, forgot to commit and pushing. So commit the files and then push.

17
votes

If you get this error while working in detached HEAD mode, you can do this:

git push origin HEAD:remote-branch-name

See also: Making a Git push from a detached head

If you are on a different local branch than the remote branch, you can do this:

git push origin local-branch-name:remote-branch-name
17
votes

Short answer: This error means the branch you want to push in remote doesn't exist!

In my case, starting from October-2020, the repos created since then had the main branch instead of the previous master branch. So all I had to do this:

git push -u origin main 
  • you may skip -u flag if the upstream is set( Like in case you had cloned it already)

Bingo! That worked for me! Hope that helps! Happy coding!

17
votes

I was facing the same issue and tried most of the answers here, But the issue was because of recent changes of Github renaming.

GitHub is gradually renaming the default branch of repositories from master to main.

https://github.com/github/renaming

Your new command would be :

git push origin main

instead of this :

git push origin master
16
votes

Maybe the branch is main instead of master

try

git push origin HEAD:main or git push origin main