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

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'

This question is related to git commit

The answer is


Permissions 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 I did execute pull, commit, and push commands once again on the forked repository.


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


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"

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.


What worked for me was simply checkout to the branch that I want my code to push and then simply push your code.

git checkout -b branchname-on-which-i-will-push-my-code

git add .
git commit -m "my commit message"
git push origin branchname-on-which-i-will-push-my-code

Only commit solved this error:

git commit -m "first commit"

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

If you are using Git Bash on Windows, try restarting it. It worked for me!


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

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


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


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


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


If you have Visual Studio Code:

  1. Delete .git folder if you don’t 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 the 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 Code wants you to enter your username and password.

If you have permissions to the repository, 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 master@{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

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.


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

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.


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


You may run into this issue for multiple reasons.

1. Pending commit for the stagged 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 name of branch,(i.e mster instead of master) then it ill lead you to this error. means the branch you're trying to push into is not in the local repository.


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


git add .

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


I too faced the same problem. But I noticed that my directory was empty when this error occurred. When I created a sample file and pushed again it worked. So please make sure before pushing that your folder is not empty!!


  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.


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

I faced similar error. The error was due to this command

git push -u origin master

subsequent commands worked for me

start with these commands

git init
git add .
git commit -m "second commit"

so before pushing it run these commands to see what remote repository on Github our local repository is connected to and which branch are you on.

git remote
git branch

remote -->origin

branch --> main

git push -u remote branch

or more specifically :

git push -u origin main

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.


I did face the same problem, but in my case the 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 Git Bash. XXXX being the username and YYYY the repository name.


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


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.


Github changed the default branch name from master to main. So if you created the repo recently, try pushing main branch

git push origin main

Github Article


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


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.


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.


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

git push origin main

You can see the result in this photo

Git problem


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

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


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.


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!


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

I think this might help someone. I made my first major commit today. I tried a lot of the guide given it kept spitting errors.

First: cd into the directory with your files Initialize git: git init commit the files: git commit To enforce the commit if there are any hazy bash commands: click 'I' to insert your commit message To continue: click 'ESC'

To send to your github repo first ensure that your username and email has been added:

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

Continue:

git remote add origin https://github repo url

To push to the repo:

git push -u origin 'https://github.repo url'

It loads the commits to your repository. DONE!!!


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


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.


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.


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

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

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

This error occurs as you are trying to push an empty repo into the git server. This can be mitigated by initializing a README.md file :

cat > README.md

Then type something, followed by an enter, and a CTRL+D to save. Then the usual committing steps :

git add .
git commit -m "Initial commit"
git push origin master

If 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

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


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'

I ran into the same snag..and the solution was to push the code to the repo 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

I had the same problem and discovered also that I had not committed any file, so when I tried to commit again, I got 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 'USER@WINDOWS-HE6I2CL.(none)')

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

git commit -m 'Initial commit'

Then pushed

git push -u origin master

Just add an initial commit. Follow these steps:

  • git add .

  • git commit -m "initial commit"

  • git push origin master

This worked for me.


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!


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"

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

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.


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

I forgot to commit then ran to this. JUST COMMIT


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.


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

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

The issue is that you have not configured git to always create new branches on the remote from local ones.

The permanent fix if you always want to just create that new branch on the remote to mirror and track your local branch is:

git config --global push.default current

Now you can git push without anymore errors!


In my case fastlane match nuke development (which deletes the development certificates and profiles from the git credential store and the Developer Portal) tried to push master but we don't have master. Since we have two different teams we have also 2 different branches. What solved the problem was to call tell match about the correct branch:

fastlane match --git_branch <your_branch> nuke development

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

git push -u origin master

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 are no commits in the repository and thus no 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

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

git commit -m "first commit"

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

This could also happen if you have a typo in the name of your branch and therefore you're trying to push something that doesn't really exists! ha!

In my case, I actually created the branch with an incorrect name (by accident, obviously) and it was called fetaure/<something> and was trying to push feature/<something>

The brain will read the word just as a unit and therefore sometimes both words look the same but they're not! ha!

Just be sure you don't have a typo in the name of the branch! (or better said, check that the branch you want to push actually matches the branch you have defined locally, specially if you are manually doing the first push of a newly created branch git push origin feature/<newBranch>)


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


  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.


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
    

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.


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

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.


make sure you are pushing to the right branch or is there any typo. check out your current working branch name with this command
git show-branch


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.

As of 1st October 2020, GitHub is changing the name of the master branch to main. This is what is causing the issue. When someone types git push origin master they see this error
error: src refspec master does not match any error: failed to push some refs to 'https://github.com/vishnureddys/coding-skills.git'

This can be fixed by using git push origin main. Hope this helps. I took quite some time to figure out why I couldn't push my commits to the master branch.


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.


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

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

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

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"

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

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

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

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

I had an extremely long local branch name.

git branch -m new_shorter_branch_name

fixed the problem.


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.


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)


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.


One reason for this month is probably be: github has rename default "master" branch to "main" branch. So, use git push origin main instead.


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.


Try git show-ref

You might see refs/heads/live

This means you should do

git push -u origin live

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.


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

git commit -m "initial commit"

Try this:

git add .

git commit -m "your commit message"

git remote add origin *remote repository URL*

git push origin *your-branch-name*

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