I cloned my repository with:

git clone ssh://xxxxx/xx.git 

but after I changed some files and add and commit them I want to push them to 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'

66 Answers

659
Vi. On
  1. Try git show-ref to see what refs do you have. Is there refs/heads/master?

  2. You can try git push origin HEAD:master as 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).

64
Andrew E On

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.

3339
baisong On

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!

12
user993563 On

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

198
Aryo On

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

My error message was something like this:

error: src refspec master does not match any.
error: failed to push some refs to '[email protected] ... .git'

and it 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.

That's it, hope this helps.

41
wilsonfoz On

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'
1
Pramod On

@Aryo In my case I had to use the full url of my local git repository to push the file. First I removed all the files in current directory. Created README added it. Added some more. Then I commited those files and at last pushed them giving proper url to the repository. Here yourrepository is the name of the repository in the server.

rm -rf *

touch README
git add README
touch file1 file2
git add file1 file2

git commit -m "reinitialized files"
git push [email protected]:yourrepository.git master --force
0
aimango On

None of the above solutions worked for me when I got the src-refspec error. My workflow:

  • pushed to remote branch (same local branch name)
  • deleted that remote branch
  • changed some stuff & committed
  • pushed again to the same remote branch name (same local branch name)
  • got src-refspec error.

Fixed error by simply making a new branch, and pushing again. (Weird thing was, I couldn't simply just rename the branch - gave me fatal: Branch rename failed)

8
asdasd On

You need to config your git if is the first time that you use it with:

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

git config --global user.name "Your Name"
63
aug2uag On

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
Vojtech Vitek On

If you want to create new branch remotely in the origin, you need to create the same branch locally first:

$ git clone -b new-branch
$ git push origin new-branch
5
Zhengming Ying On

I think its because you pushed an invalid branch. Generally because the repository does not have common master branch(maybe development branch). You can use git branch to see branches.

165
tldr On
  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.

2
equivalent8 On

I was contributing to one github repo so I forked the project, cloned it, created my own branch, did some commits and tried to push. At this point I discovered that I cloned not my fork but the original project repo (which I don't have permission to push).

So I changed the .git/config to point origin to my repo.

at this point when I tried to push I was getting error error: src refspec my_awesome_branch does not match any.

All I had to do is to touch any file and commit it (similar like you see it in this answer

git touch README
git commit -m "fixing error in my git repo"

and than:

git checkout master
git pull origin master
git push origin master # this will tell my remote repo about my new branch
git checkout my_awesome_branch
git push origin my_awesome_branch # now it will work
9
Gavin On

This will also happen if you have a typo in the branch name you're trying to push.

4
Paket2001 On

I had a similar error. But git tells me:

*** Please tell me who you are.

Run

git config --global user.email "[email protected]"
git config --global user.name "Your Name"

Or to set your account's default identity.

Omit --global to set the identity only in this repository.

Then the error goes away.

6
Giulio Roggero On

This worked for me resetting to remote master the repo

git checkout master
git commit -a -m "your comment"
git push origin master
3
espradley On

Old topic, but I had this issue and wanted to share my experience. I had already created a commit.

Make sure you are pushing to the right branch

I was typing git push origin master but when I typed git branch I was on a v1 branch, so I had to type git push origin v1

17
xuri On

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

git add .
git commit -m 'initial commit'
git push origin master
0
Thomas Jensen On

I just got this error while trying to push stuff into a new repo on GitHub. I had created the git repo locally, plus I had created the repo on GitHub using the Web GUI (including a LICENSE file).

The problem went away after I pulled the LICENSE file from the otherwise empty GitHub repo into my local repo. After that, I could push with no problems.

102
Red On

For me I had to make sure the public key is properly configured in 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

Worked for me in the end.

0
arush436 On

I also received this problem but it was because I accidentally shut down my server before doing the push. This too will cause the same error.

17
Antoine On

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 repo.

58
pratik kumar On

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

git init
git add .
git commit -m 'message'
git push -u origin master
1
Scotty.NET On

I had an extremely long local branch name.

git branch -m new_shorter_branch_name

fixed the problem.

4
Gujarat Santana On

I have the same issue, and fix mine in the following step :

hope it helps

1
adrian filipescu On

Error from git: error: src refspec master does not match any.

fixed with this step: git commit -m "first commit"

before I ran: git add

and after I ran: git push -u origin master

!!!

17
Alwan Mortada On
  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.
5
jcw On

Another possible cause of this problem is if you misspell the branch name. So if you did what I did then the problem would be fixed by correcting:

git push origin mater

to

git push origin master
6
Amalgovinus On

I was getting this error because my local branchname did not match the new remote branch I was trying to create with git push origin <<branchname>>.

8
Lackeeee On

I was forgot to do a "git pull origin master" after commit and before push and it cause the same problem: "src refspec master does not match any when pushing commits in git". So, what you should to do is:

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

I have same problem. I do it by 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

Hope it will help anyone

4
ihayet On

This happened to me when I did not refer to the master branch of the origin. So, you can try the following:

git pull origin master

This creates a reference to the master branch of the origin in the local repository. Then you can push the local repository to the origin.

git push -u origin master
3
Ali On

I created the files in wrong directory and tried to do git push -u origin master and I got the error Once I cd to current directory do git push -u origin master all is fine.

0
kamilk On

In my case the issue, occuring on Windows, seemed to have something to do with us adding a prefix like feature\ to branch names. We were trying to create and push a branch with such a prefix (say, feature\branch) but there was already a different branch, with a different name prefixed with Feature\ (say, Feature\otherbranch). This means that on Windows the new branch was placed in the same refs\heads\Feature folder. Git may be case-sensitive but Windows filesystem isn't. It helped once we checked out the local branch named Feature\branch.

11
HuntsMan On

git add .

is all you need that code track all untrack files in your dir

9
Govind On

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

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

Your code will be pushed as new branch.

15
neoDev On

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)

3
Sina Madani On

The problem I had was when trying to reset my repository. I wanted to delete all history and commit nothing. However, you have to add at least SOMETHING to commit, so I just created an empty text file, git add . and then git commit -m "Reset repository".

7
d.raev On

In the scenario where you checkout out a repo from external source, and want to import it in personal / internal system, this command really shines:

git push --all origin

This pushes all branches, without checking refs, insisting on commits.

2
Dimpy Chhabra On

I faced this exact problem while dealing with VCS in Android Studio. Turns out all I had to do was:

  1. Select all files from the "app" folder;
  2. Go to VCS (Option at top);
  3. "Add" the files;
  4. Committing again via terminal, or by clicking via the drop down menu, and;
  5. Push!

Eureka! :D

101
VIKAS KOHLI On
git push -u origin master
error: src refspec master does not match any.

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

git commit -m "initial commit"

git push origin master

Succesfully pushed to master

6
Xia Sheng On

I got this problem while adding an empty directory. Git doesn't allow to push empty directory. Here is a simple solution.

Create the file .gitkeep inside of directory you want to push to remote and commit the "empty" directory from the command line:

touch your-directory/.gitkeep
git add your-directory/.gitkeep
git commit -m "Add empty directory"
0
Janac Meena On

For users of Bash within Cmder on Windows, make sure to create a new .ssh folder in your new home directory.

1) Go to your home directory cd ~.

2) Generate ssh keys ssh-keygen.

3) Leave all inputs blank (keep pressing enter)

4) Copy the id_rsa.pub file into your Github > Settings > SSH Keys

25
NeeruSingh On

just add a initial commit ,follow steps:-

  • git add .

  • git commit -m "initial commit"

  • git push origin master

    This worked for me

7
Palak Jain On

Try this:

git add .

git commit -m "your commit message"

git remote add origin *remote repository URL*

git push origin *your-branch-name*
1
Laxminarayan Nayak On

In my case i cloned repository but i didn't switch to the branch locally

i solved by doing this

before making changes in code you should do this git checkout branch-name

then make changes to your code

after that push the code to branch git push -u origin branch-name

1
discipleartem On

I am catched this "error: src refspec master does not match any." when tried to push commit to GitHub, having changes (at GitHub).

git push -u origin branch-name - helped me up to date my local files
10
snap On

If you get this error while working in detach 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
1
Sohail On

Work for me

Just checkout master branch

git checkout -b master
git add .
git push origin master 

Or use --force for force change

git push origin master --force
6
Ferhat KOÇER On

check your commit title because if you forget git commit -m "xxxx" command, you get the same problem

git commit -m "initial commit"
6
Anubhav Pandey On

Did face the same problem but in my case following the exact steps from the beginning as given on the page when you create a new Repository worked. Just pasting that over here

  echo "# YYYY" >> README.md
  git init
  git add README.md
  git commit -m "first commit"
  git remote add origin https://github.com/XXXX/YYYY.git
  git push -u origin master

Type the above in GitBash XXXX being the username and YYYY the repository name.

47
Saurabh Singh On

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
1
samezedi On

I had the same problem and discovered also that I had not committed any file, so when I tried to commit again I get this error message

*** Please tell me who you are.

Run

 git config --global user.email "[email protected]"
 git config --global user.name "Your Name"

 to set your account's default identity.
 Omit --global to set the identity only in this repository.

 fatal: unable to auto-detect email address (got '[email protected](none)')

then all I did was add my email and name globally and the committed again

git commit -m 'Initial commit'

then pushed

git push -u origin master

I hope this helps

5
Omid Ahmadyani On

only commit solved this error.

git commit -m "first commit"
0
M. Dhaouadi On

I had this error, and it was a problem with the name of the branch because I used the character "&". I just skipped it by "^&" and it worked.

0
oskarko On

It doesn't recognize that you have a master branch, just create it and commit again.

To create a master branch:

git checkout -b master
16
Jin Kwon On

I faced same problem and I used --allow-empty.

$ git commit -m "initial commit" --allow-empty
...
$ git push
...
1
Amir.S On

Maybe github doesn't know who you are.

first you have to run : git config --global user.email "[email protected]" git config --global user.name "Your Name"

0
saber tabatabaee yazdi On

if you have visual studio code

  1. delete .git folder if you dont need changes to track ( if yes git stash)
  2. git init
  3. git commit -m "first commit"
  4. git remote add origin https://github.com/YOURusername/YOURrepo.git
  5. by Visual Studio Code UI IDE GOTO source control from left hand side panel or (Ctrl + shift +G)
  6. stage all your changes or part
  7. commit your changes
  8. Synchronize your changes by left bottom button ( like number or like cloud icon) then visual studio want you to enter your username and password

if you have permission to repo you can see :

> git push -u origin master
Fatal: AggregateException encountered.
To https://github.com/your_account/yourrepo.git
 * [new branch]      master -> master
> git status -z -u
> git symbolic-ref --short HEAD
> git rev-parse master
> git rev-parse --symbolic-full-name [email protected]{u}
> git rev-list --left-right master...refs/remotes/origin/master
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose
> git show :ionic.config.json
> git check-ignore -z --stdin
0
ExillustX On

If you are using Git Bash on Windows try restarting it. Worked for me!

4
rabbitwerks On

Double check that you're pushing the correct branch name. i encountered the same error and after looking at git show-ref i was able to see i was typing it in wrong, therefore, no ref.

0
MAX On

Permission Issue? Resolution Fork it

I got this error because i had no push permission to the repository so i had to Fork it and then did execute pull, commit, push commands once again on the forked repo.

11
Gel Sisaed On

I also followed githubs directions as follows below but still faced this same error as mentioned by 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. Typed git add . and wait for the files to finish being added. Then follow through the next steps.

I hope this helps someone.

11
Sumer On

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

0
Ismail H On

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

  1. Create an empty git repo 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