Message 'src refspec master does not match any' when pushing commits in Git

4.5m Views Asked by At

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'
50

There are 50 best solutions below

22
Vi. On
  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).
7
Andrew E On

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.

32
baisong On

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!

0
user993563 On

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

4
Aryo On

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

Regarding Aryo's answer: 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 the current directory and created README added it.

Added some more. Then I committed those files and at last pushed them, giving a proper URL to the repository. Here yourrepository is the name of the repository on the server.

rm -rf *

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

git commit -m "reinitialized files"
git push git@localhost: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.

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

0
asdasd On

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"
4
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
0
Vojtech Vitek - golang.cz On

If you want to create a 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
0
Zhengming Ying On

I think it's because you pushed an invalid branch.

Generally, because the repository does not have a common master branch (maybe development branch). You can use

git branch

to see branches.

0
equivalent8 On

I was contributing to one GitHub repository, 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 repository (which I don't have permission to push to).

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

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

All I had to do was 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 then:

git checkout master
git pull origin master
git push origin master # This will tell my remote repository about my new branch
git checkout my_awesome_branch
git push origin my_awesome_branch # Now it will work
1
Gavin On

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

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

0
Giulio Roggero On

This worked for me, resetting to remote master the repository:

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

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.

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

I just got this error while trying to push stuff into a new repository on GitHub. I had created the Git repository locally, plus I had created the repository 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 repository into my local repository. After that, I could push with no problems.

0
pyfork On

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

0
Anthony 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 repository.

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

0
Gujarat Santana On

I had the same issue and fixed it using the following steps:

0
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 <files>

And after I ran:

git push -u origin master
0
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.

0
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
0
Alkanshel 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>>.

0
Lackeeee On

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
0
user3051460 On

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
0
Ishrak 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
0
Ali On

I created the files in the wrong directory, tried to do git push -u origin master, and I got the error.

Once I cd to the current directory, do git push -u origin master, and 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.

0
HuntsMan On
git add .

is all you need. That code tracks all untracked files in your directory.

0
Govind On

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.

0
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.)

0
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".

0
d.raev On

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.

0
Dimpy Chhabra On

I faced this exact problem while dealing with VCS in Android Studio. It 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

2
VIKAS KOHLI On
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.

0
c__c On

I got this problem while adding an empty directory. Git doesn't allow to push an 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

0
NeeruKSingh On

Just add an initial commit. Follow these steps:

  • git add .

  • git commit -m "initial commit"

  • git push origin master

This worked for me.

0
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*
0
Laxminarayan Nayak On

In my case I cloned a repository, but I didn't switch to the branch locally.

I solved it 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 the branch:

git push -u origin branch-name

Also, if you are pushing your local repository first time to GitHub, you need to first create a main branch:

git branch -M main

And, then, after adding the origin (or whatever name you give to your remote) push the branch:

git push -u origin main
0
discipleartem On

I got this error,

error: src refspec master does not match any.

when I tried to push a commit to GitHub, having changes (at GitHub).

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

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
0
Sohail On

This works for me:

Just checkout the master branch:

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

Or use --force for forcing a change.

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

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

git commit -m "initial commit"