4038

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
  • 71
    @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. 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 Jul 2, 2015 at 13:00
  • 47
    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
    Sep 16, 2017 at 9:28
  • 12
    If you didnt add git add with dot or some files this error also will appear.
    – Blasanka
    Apr 28, 2018 at 10:18
  • 34
    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
    Nov 23, 2020 at 4:39

140 Answers 140

6

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

git commit -m "initial commit"
1
  • 1
    No, I mean if you forget git commit -m "xxxx" command, you get the same problem.Thank you I edited my comment Apr 3, 2018 at 19:47
6

As one of the options for solving your problem:

git push origin HEAD
6

In 2020:

If none of the 30+ answers has worked, you probably need to run git push origin main (master has been renamed to main at the time of writing this answer).

3
  • It was 92 answers (incl. deleted). Sep 18, 2022 at 18:03
  • It would be better to tie it a (exact) Git version number (in addition to the date) - if that applies, or an announced change. In any case, information about the exact source of the change (Git? GitHub? Something else?). Some may be using older versions, incl. being on old versions of operating systems. Sep 18, 2022 at 18:05
  • Another answer indicates the origin of the change is on GitHub. Sep 18, 2022 at 18:10
6

I ran into the same snag. The solution was to push the code to the repository as though it were an existing project and not a brand new one being initialised.

git remote add origin https://github.com/Name/reponame.git
git branch -M main
git push -u origin main
5

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
5

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.

5

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.

1
  • I migrated to a new machine and was getting these errors. Setting my GIT info fixed it.
    – swt83
    Jan 5, 2016 at 16:15
5

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"
1
5

Try git show-ref

You might see refs/heads/live

This means you should do

git push -u origin live
3
  • 1
    I think you mean "main" not "live". As of June 2020 they've dropped "master" in favor of "main. Oct 10, 2020 at 2:13
  • Hi @JosephBrenner, I already removed the reference to Github.
    – jflaga
    Oct 13, 2020 at 3:28
  • 1
    Tried this solution and got refs/heads/master. Then git push origin master worked.
    – Jacobs2000
    Nov 6, 2020 at 8:37
5

I had the same issue just today. I created a new repo and cloned it to my machine. I committed my code and tried to push it. I got the same error. I observed that it is because I was using:

git push origin master

What I was doing wrong here is that I assumed my default branch to be master whereas the new default on GitHub is main. I pushed using:

git push origin main

and it worked fine.

My solution applies only to the newer repos or people facing this issue very recently because GitHub is replacing the main over master terminology. So if you get this error, make sure to check the branch you are pushing to and the branch name on GitHub.

2
  • I am getting the opposit , I have seen that my default is main but when I try men I get error what do you think I should do Oct 27, 2020 at 16:56
  • As far as I could understand, this is because you haven't created any branch named men. If you want a branch named men you can use git checkout -b men and then you would be able to push this branch to remote.
    – Ayush Jain
    Oct 30, 2020 at 21:03
5

I forgot to commit and then ran into this. Just commit.

2
5

For Repositories WIKI, I encountered this error also.

git show-branch

if shows master then

git push -u origin master
2
  • What about if it doesn't shown anything Feb 15, 2022 at 14:13
  • What is "Repositories WIKI"? Can you elaborate and/or provide a reference? May 18 at 21:28
4

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

4

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
  • If you want to push your current branch, an easy way to do it is to simply do git push origin HEAD, or git push origin @ if you're using a recent version of Git, or git push origin head if you're using Windows or OS X.
    – user456814
    Jun 17, 2014 at 16:59
4

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

4

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.

4

I had this problem once because i had a branch on my remote repo but not locally. I did:
git fetch && git checkout 'add remote branch name here' and it solved my problem.
Sometimes the problem occurs when you don't stage your changes, so to do this you need to run the following git command:
git add your_file_name.extension or git add . to add all changes.
At this point you need to commit your changes with:
git commit -m 'your commit message here'.
Once you have done all that, you just need to push your changes to remote repo with:
git push origin your_branch_name.

4

First you need to git init to create your own .git file, otherwise if you clone someones git folder it will not recognize your git credential. After you started git, then continue with git add. and git commit ...

4

For GitLab users, the updated version of GitLab will now have a 'main' branch as the default.

So you can try the following:

git push origin main

Reference: GitLab new Git default branch name is main

4

You may run into this issue for multiple reasons.

1. Pending commit for the staged files

If you've added the changes by running git add command (i.e., git add .), and never committed those files then after and tried to push the branch into the remote repository. In this case, you'll face the error src refspec master does not match any.

2. Invalid local branch name

If you did a typo in the name of the branch, (i.e., mster instead of master), then it will lead you to this error. It means the branch you're trying to push into is not in the local repository.

0
4

First of all, make sure that you are using the master branch. In my case, the branch was main instead of master. I did:

git push origin main

You can see the result in this photo:

Git problem

1
4

One reason for this month is probably be: GitHub has renamed the default "master" branch to "main" branch.

So, use git push origin main instead.

0
4

Update to previous answers.

Also, don't forget that GitHub has changed 'Master' to 'Main', so make sure you're pushing via:

git push origin main
2
  • 1
    That is valid only for new repositories. So this answer is completely inaccurate. More info here: github.com/github/renaming
    – zinovyev
    Oct 23, 2020 at 22:48
  • 2
    I don't think its inaccurate at all. I came here because I received the same error message. The error happened to be that "master" was changed to "main" on the remote side and I never updated the remote URL. Oct 25, 2020 at 20:08
3

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
1
  • Why did you need to use the full URL for your remote instead of just setting up an alias for it with git remote add origin <url>?
    – user456814
    Apr 4, 2014 at 20:53
3

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
3

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
3

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

3

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

3

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
3

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.