4254

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
  • 81
    @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

4

I faced a 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 
Sign up to request clarification or add additional context in comments.

Comments

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 

Comments

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

Comments

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

Comments

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 

Comments

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 

Comments

3

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 

Comments

3

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 any more errors!

Comments

3

This was an error I got when I started to learn Git. The cause of this error is when you add a remote origin to a Git repository, Git on the system puts it on the master branch, but GitHub has moved from master to main here.

To solve this issue:

Use git checkout -b main to switch to the main branch. Use git pull origin main to pull the main branch, and add proper flags if you want to merge or rebase the branches. Now you can use git push origin main to push your work to the main branch.

A few months later...

I just found another cool way of doing this:

git config --global init.defaultBranch main 

Using this, you can get rid of the error for all later times. To test it out, enter the above command and initialize an empty Git repository locally, make a random file in it, and do git add . and git commit -m "message".

To check the branch name, do git branch. It should now show main.

Comments

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 Comment

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

Comments

3

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 

Comments

3

I recently came across this problem. I checked show-ref using git show-ref and checked whether

refs/heads/master

was present.

It was present, but still it was not working for me.

As I was writing

git push origin main 

Later I changed this command with

git push origin master

and it worked perfectly fine for me.

1 Comment

push is always done on that branch which is selected.
3

March 2021, with Git 2.31:

A git clone of an empty GitHub repository will create a local repository with 'main' as a default branch, even if init.defaultBranch is set on master!

The Git transfert protocol v2, supported by GitHub, will communicate to the client the name of the remote default branch (now main for GitHub) to the local Git repository created by git clone.

That means adding and committing new commits will be done on 'main' branch, even if the Git client still consider master as the default branch.

No more "src refspec master does not match any." on your next push.

my suggestion is, after using these codes:

git init git remote add origin <your-repo-url> 

clone your repo by this code:

git clone <your repo url> 

then cd repo-folder and copy the .git and all content to your main directory (cd ..) and delete the repo-folder. Now you can add, commit and push your files.

Comments

2

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

Comments

2
  1. git branch

  2. Rename the branch with the -mv flag:

    git branch -mv origin master

After this, git branch should show master.

2 Comments

The purpose of git branch ought to be explained. Please respond by editing (changing) your answer, not here in comments (but *** *** *** *** *** without *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today).
2

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 

Comments

2

I made my first major commit today. I tried a lot of the guide given, but 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 repository, 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 repository:

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

It loads the commits to your repository.

Done!!!

Comments

2

This issue can be solved by reinitializing Git in the project folder:

# Type the following on the CMD git init # Then try pushing your code online git push 

Comments

1

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.

Comments

1

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.

1 Comment

Re "skipped": Do you mean "escaped"?
1

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

Comments

1

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.

Comments

1

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 

Comments

1

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

Comments

1

Also check if you have changed the branch name

For me, I had changed the branch name from add-status-conditions to add-status-condition and failed to notice the missing s at the end. Renamed the branch correctly and did git push origin add-status-conditions -f.

That worked for me.

Comments

1

Updated answer: Make sure all changes are committed.

Then type : git push Before this make sure to make a personal access token, GitHub uses this now. Then type out your GitHub username (after typing git push) then paste your personal access token in the password input.

After all this you should be able to see your changes in GitHub!

Comments

1

I was facing this issue because I had made a branch and I had to use that branch and after that it was resolved by using this.

git push origin branch_name

Comments

1

Make sure you do not have an invisible typo in a branch name:

git branch 

Output:

master * <U+0096>your_branch_name 

There is an invisible <U+0096> character which probably got there by pasting the branch name.

Rename your current branch to the correct value and then try push again:

git branch -m your_branch_name 

1 Comment

U+0096 is listed as <control>. What is the most likely equivalent? ASCII 0x16 (SYNC / SYN? AKA Ctrl + V (presumably in a terminal context). It would not be uncommon to use Ctrl + V in a terminal window).
1
git init git add . git commit -m "message" prompt me to enter email and user name git config --global user.email "[email protected]" git config --global user.name "Your Name" 

After entering the above details, the files are commit and pushed successfully:

git push 

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.