4119

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
  • 73
    @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
  • 50
    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
  • 35
    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

141 Answers 141

20

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.

18

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
2
  • Thanks, this works for me. other solutions didn't. Jun 26, 2021 at 3:59
  • Same happened to me just now. I just used master and it worked, instead of main. Nov 30, 2023 at 17:28
17

In 2021, GitHub changed the default branch name to main. Previously it was master.

I suffered because I tried to push to master which did not exist and the branch at remote was main instead. Make sure you are using the correct branch name.

The command below worked for me

git push origin main
1
17

Short answer: This error means the branch you want to push in remote doesn't exist!

In my case, starting from October 2020, the repositories created since then had the main branch instead of the previous master branch. So all I had to do was 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!

3
  • 2
    Even that failed. My local branch was master. I created a main locally and then push worked with -u flag
    – aahnik
    Oct 3, 2020 at 15:34
  • 2
    Thanks a lot. It worked. This was really a major recent update by github Oct 6, 2020 at 19:17
  • Thanks, this was also a recent update for gitlab, just in case anyone was wondering.
    – Heath
    Nov 12, 2020 at 20:40
16

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
15

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
14

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

1
  • 3
    I suspect a great many of us who came here via web search actually mistyped the name!
    – sage
    Jun 23, 2014 at 2:49
13
git add .

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

12

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

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
12

At the end of 2020, GitHub changed its master branch to main branch.

I noticed GitHub created a new branch master and this is not the main branch when I am using git push -u origin master:

Now when I try to use git push -u origin main, to push directly to main branch it gives me this error:

I faced this error:

src refspec main does not match any
error: failed to push some refs to 'https://github.com/<my_project_name>.git

I fixed it using these steps after my first commit to main.Change URL for your GitHub in the following code:

git branch -M main
git remote add origin https://github.com/Sidrah-Madiha/<my_project_url>.git
git push -u origin main
1
10

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.

10

May be your current branch has no upstream branch. Try these commands when you are going to push for the first time.

git init
git add .
git status
git commit -m "initial commit"
git remote add origin "github.com/your_repo.git"
git push --set-upstream origin master
10

Consider:

git push -u origin master

Output:

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 aren't any commits in the repository and thus there isn't any 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
0
9

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
9

To avoid getting into this error in 2021 and onwards, use this command before using git init

git config --global init.defaultBranch main This tells your git to use main as the default branch name always, instead of master

9

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

cat > README.md

Then type something, followed by Enter, and a Ctrl + D to save.

Then the usual committing steps:

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

Try this:

git add .

git commit -m "your commit message"

git remote add origin *remote repository URL*

git push origin *your-branch-name*
8

Only commit solved this error:

git commit -m "first commit"
8

I also faced the same issue. In my case by mistake I made a commit before adding. After performing the steps in sequential order, I got it.

Also please make sure to give the correct branch name.

git init
git add .
git commit -m <Your commit message>
git remote add origin "your repository link here"
git push -u origin master
8

I got the same issue when I was doing my first push to a repository. I forgot to commit any changes and was trying to do

git push -u origin main

This was resolved by adding a commit and then execute:

git push -u origin main
7

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

7

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

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

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

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.

0
7

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
7

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
7

I had faced the same issue before. The issue was with my local branch name. It was different from the one I tried to push. Correcting branch name to the target one, I was able to push the changes.

Remember the local branch and target branch should be same.

2
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
    – east1000
    Sep 24, 2021 at 14:00
  • I don't know why you guys said like the answer is not valid, recently I had faced the same issue as mentioned the question and resolved by the changing the branch. Just posted here to help others who go through the same problem Sep 25, 2021 at 15:42
7

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
1
  • 1
    Had the same problem and it turn out that I typo branch name.
    – Pyae
    Jun 26, 2020 at 3:32
6

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

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