4252

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'
21
  • 79
    @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. Commented Jul 8, 2013 at 0:42
  • 4
    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 project Commented Jul 2, 2015 at 13:00
  • 52
    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. Commented Sep 16, 2017 at 9:28
  • 14
    If you didnt add git add with dot or some files this error also will appear. Commented Apr 28, 2018 at 10:18
  • 43
    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. Commented Nov 23, 2020 at 4:39

105 Answers 105

6064

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

mkdir repo && cd repo
git init
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 main

Success!

Sign up to request clarification or add additional context in comments.

32 Comments

Don't just follow this step blindly, look at what @Vi has mentioned, and then modify your push command to correct ref.
The most probable reason for this error is that all the files are untracked and have not been added. git add --all in case you wish to add all the files Or you can selectively add files. Then git commit -m "Initial comment", git push origin master. This will surely work.
Fixes different issue which is nice but not really an answer to This actual question.
git commit -m 'initial commit' should be double quoted. 'git commit -m "initial commit", at least on windows.
Another possible reason: you don't actually have a branch called master
|
1829
  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).

25 Comments

my master branch wasn't on top of commits ! so i created a branch that it was at the end of all branchs and i pushed them to the server:
git checkout -b testbranch ; git push origin testbranch:master
git show-ref showed my branch; the git push origin HEAD:<branch> worked for me.
You just saved me Vi. Thank you for git push origin HEAD:master. But why something like git push --force origin master does not work?
master is changed to main now.
|
292

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) or you can also use # git add --all :/
git commit -m 'reinitialized files'
git push origin master --force  # <- caution, --force can delete others work.

4 Comments

The other answers did not solve the problem I was having (for instance, I had already committed and still had this error), but doing a git push origin BRANCH --force worked. Thank you!
See this earlier answer. I suspect that you needed to add a file because git won't track empty directories.
push --force could also completely blew away co-workers hard work. Added warning by it.
This solved my problem. I think git add did it. While pushing things at first git doesn't recognize things, may be that's why I had the problem. git add command solved my problem. also after that i was able to push without --force. Thanks Aryo
155
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.

2 Comments

I checked it's working. Please ignore -u option and then try
The issue here seems to be completely different than the OP's...but it seems many including me had this issue.
143

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

1 Comment

well commit was my head so i had to "git push origin commit" instead of changing my head to either of the branch main or master and it still work.
137

This happened to me in a brand new repository after I ran git add with only an empty directory.

As soon as I added a file (e.g. a git add README.md), then git push worked great.

5 Comments

This probably works because git doesn't actually track directories, only files. So if a directory is empty, git won't actually add it.
The OP added a file (xx.php) so this was not the problem in this case even though in other cases this can be a problem and adding a file a solution of that problem.
Such a simple solution to a frustrating problem. I was testing the creation and clonining of repos and created empty directories not files.
In my case, 1--> git init 2---> git add origin....etc 3---> git git push -u origin master ===>Then I got the above error. ===>Then I executed following 2 commands, it's disappear. ---> git add * ---> git commit -m "Some message" --->git git push -u origin master ===>Worked fine for me, in my case.
@rao yep, adding an origin isn't enough, you must add and commit files. But why do you have "git git push..."? Is the second "git" a typo?
82

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

4 Comments

that's correct, the business end is more specifically git remote add __REMOTE_NAME__ __URL_OR_SSH__, and above the remote name is "origin"
They added and committed in their question so that was not the issue even though this helps other people.
This answer did work however because of the -u option that was used here.
What’s git *create remote?
75

Problem faced

I had the same problem when I was creating a new repository on GitHub and linking it with my React app in the client computer I have.

I used the following steps:

Commands used before the problem

git init
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main

My mistake

But as you can see, my mistake was not using the git add . command. I did this mistake, because I already had the README.md file and GitHub instructs us with basic commands while creating the repository.

My solution

My solution is to use git add . after the git init command.

Use the following set of commands in the same order to overcome the problem:

git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin "_git repository link here_"
git push -u origin main

3 Comments

Run git add . then git commit -m "first commit", then push again
git branch -M main worked for me.
This git branch -M main is important which is why it wasn't working for me. It expects master if you don't put anything.
74

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

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

Comments

72

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'

7 Comments

LOL. I was trying to push to origin master but that branch didn't exist. It was called origin stable.
The -u may have helped here.
In the above case, the problem is of course that there's no local branch master, so you can't push it. You either want to push an existing branch – or create the master branch and then push it, like this: git checkout -b master; git push -u origin master;
I just got this when I misspelled the branch name.
My local branch was spelled "sheduler" and I was doing git push origin scheduler. HA! One letter off will kill you in programming. lol
|
66

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.

1 Comment

This is the only command that worked out of all of them.
64

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

Comments

48

Two possibilities:

1- Either you 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
    

2- Or you are trying to create a new GitHub project.

GitHub replaced master with main as the default branch name. To resolve the issue:

  1. On your local project:
    1. remove the .git folder if it exists
    2. recreate a clean repository by launching the following in your project:

In the terminal:

git init

git add .

git commit -m "YOUR FIRST MESSAGE HERE"

git branch -M main

git remote add origin _GIT_LINK_TO_PROJECT_HERE_

git push -u origin main

1 Comment

How is this the error in question related to .gitignore?! If it is related (which I highly doubt) you should explain it in your answer. Thanks
44

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.

1 Comment

did you mean ` git add --all` with two dashes
30

If your branch is called "main":

enter image description here

Run this command:

git push origin main

If your branch is called "master" instead, run this command:

git push origin master

1 Comment

What is up with the bumping, changing the date without changing anything else?
28

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.

Comments

22

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

Comments

22

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

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

Comments

18

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

In my case, starting from October 2020, the repositories created since then had the main branch instead of the previous master branch. So all I had to do was 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!

2 Comments

Even that failed. My local branch was master. I created a main locally and then push worked with -u flag
Thanks, this was also a recent update for gitlab, just in case anyone was wondering.
17

In the scenario where you check out the code from an external repository (GitHub), and want to import it in personal / internal system, this command really shines:

git push --all origin

This pushes all local branches to the remote, without checking refs and without insisting on commits.

Comments

16

I had the same problem. I did it by the following steps:

1. git commit -m 'message'
2. git config --global user.email "your mail"
3. git config --global user.name "name"
4. git commit -m 'message'
5. git push -u origin master

Comments

16

All you need is:

git add .

That code tracks all untracked files in your directory.

Comments

14

You need to configure your Git installation if it is the first time that you use it, with:

git config --global user.email "[email protected]"

git config --global user.name "Your Name"

Comments

13

At the end of 2020, GitHub changed its master branch to main branch.

I noticed GitHub created a new branch master and this is not the main branch when I am using git push -u origin master:

Now when I try to use git push -u origin main, to push directly to main branch it gives me this error:

I faced this error:

src refspec main does not match any
error: failed to push some refs to 'https://github.com/<my_project_name>.git

I fixed it using these steps after my first commit to main.Change URL for your GitHub in the following code:

git branch -M main
git remote add origin https://github.com/Sidrah-Madiha/<my_project_url>.git
git push -u origin main

1 Comment

I gave a similar answer that helped me on this question: stackoverflow.com/a/66737426/5996491
13

In case if you are facing this problem even after doing git init and pushing your initial commit. You can then try the following,

git checkout -b "new branch name"
git push origin "new branch name"

Your code will be pushed as a new branch.

Comments

12

I had the same issue. Turns out that I was pushing to master branch, while the original branch was main. You can check it using git show-ref.

If it shows refs/head/master, just change your branch to main. Here is what you can do:

git add .
git commit -m "message"
git branch -M main
git push -u origin main -f

Comments

11

Consider:

git push -u origin master

Output:

error: src refspec master does not match any.
error: failed to push some refs to 'http://REPO.git'

This is caused by the repository still being empty. There aren't any commits in the repository and thus there isn't any master branch to push to the server.

It worked for me

Resolution

  1. git init
  2. git commit -m "first commit"
  3. git add app
  4. git commit -m "first commit"
  5. git push -u origin master

Comments

11

May be your current branch has no upstream branch. Try these commands when you are going to push for the first time.

git init
git add .
git status
git commit -m "initial commit"
git remote add origin "github.com/your_repo.git"
git push --set-upstream origin master

Comments

9

I forgot to do a "git pull origin master" after commit and before push and it caused the same problem: "src refspec master does not match any when pushing commits in git".

So, you should do:

1. git add .
2. git pull origin master
3. git commit -am "Init commit"
4. git push origin master

Comments

9

To avoid getting into this error in 2021 and onwards, use this command before using git init

git config --global init.defaultBranch main This tells your git to use main as the default branch name always, instead of master

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.