相关文章推荐
深情的煎鸡蛋  ·  the value of the ...·  2 周前    · 
傻傻的甘蔗  ·  [PHP] ...·  1 年前    · 
温柔的沙滩裤  ·  apache 2.4 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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'
                @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.
– tandrewnichols
                Jul 8, 2013 at 0:42
                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
– Samitha Chathuranga
                Jul 2, 2015 at 13:00
                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
                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

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!

Don't just follow this step blindly, look at what @Vi has mentioned, and then modify your push command to correct ref. – Kumar Jun 7, 2012 at 16:43 The most probable reason for this error is that all the files are untracked and have not been added. git add --all in case you wish to add all the files Or you can selectively add files. Then git commit -m "Initial comment", git push origin master. This will surely work. – Bhanu Pratap Singh May 20, 2015 at 7:57 git commit -m 'initial commit' should be double quoted. 'git commit -m "initial commit", at least on windows. – dance2die Jun 11, 2016 at 13:12

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

  • 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).
  • my master branch wasn't on top of commits ! so i created a branch that it was at the end of all branchs and i pushed them to the server: – sinoohe Nov 17, 2010 at 4:26 You just saved me Vi. Thank you for git push origin HEAD:master. But why something like git push --force origin master does not work? – shkschneider Jul 17, 2012 at 14:09

    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.
                    The other answers did not solve the problem I was having (for instance, I had already committed and still had this error), but doing a git push origin BRANCH --force worked. Thank you!
    – Lemmings19
                    Mar 5, 2013 at 1:35
                    See this earlier answer. I suspect that you needed to add a file because git won't track empty directories.
    – user456814
                    Apr 4, 2014 at 20:57
                    This solved my problem. I think git add did it. While pushing things at first git doesn't recognize things, may be that's why I had the problem. git add command solved my problem. also after that i was able to push without --force. Thanks Aryo
    – Anandaraja_Srinivasan
                    Oct 4, 2015 at 13:36
    

    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.

    The issue here seems to be completely different than the OP's...but it seems many including me had this issue. – ZX9 Dec 11, 2019 at 1:18

    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 probably works because git doesn't actually track directories, only files. So if a directory is empty, git won't actually add it.
    – user456814
                    Apr 4, 2014 at 20:51
                    The OP added a file (xx.php) so this was not the problem in this case even though in other cases this can be a problem and adding a file a solution of that problem.
    – Michael Durrant
                    Jun 8, 2015 at 0:21
                    Such a simple solution to a frustrating problem. I was testing the creation and clonining of repos and created empty directories not files.
    – James Wierzba
                    Sep 18, 2015 at 14:14
                    In my case, 1--> git init 2---> git add origin....etc 3---> git git push -u origin master  ===>Then I got the above error. ===>Then I executed following 2 commands, it's disappear.  ---> git add * ---> git commit -m "Some message" --->git git push -u origin master    ===>Worked fine for me, in my case.
    – Kodali444
                    Dec 3, 2019 at 9:14
    

    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://yourusername@github.com': 
    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
                    that's correct, the business end is more specifically git remote add __REMOTE_NAME__ __URL_OR_SSH__, and above the remote name is "origin"
    – aug2uag
                    Jul 17, 2014 at 18:32
                    They added and committed in their question so that was not the issue even though this helps other people.
    – Michael Durrant
                    Jun 8, 2015 at 0:18
    

    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'
                    LOL. I was trying to push to origin master but that branch didn't exist. It was called origin stable.
    – penner
                    May 27, 2013 at 23:35
                    In the above case, the problem is of course that there's no local branch master, so you can't push it. You either want to push an existing branch – or create the master branch and then push it, like this: git checkout -b master; git push -u origin master;
    – tanius
                    Dec 12, 2016 at 0:01
                    My local branch was spelled "sheduler" and I was doing git push origin scheduler.  HA!  One letter off will kill you in programming. lol
    – protoEvangelion
                    Jul 17, 2017 at 17:11
    

    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.

    Problem faced

    I had the same problem when I was creating a new repository on GitHub and linking it with my react-app in the client computer I have.

    I used the following steps:

    Commands used before the problem

    git init
    git commit -m "first commit"
    git branch -M main
    git remote add origin "_git repository link here_"
    git push -u origin main
    

    My mistake

    But as you can see my mistake was not using the git add . command I did this mistake because I already had README.md file and GitHub instructs us with basic commands while creating the repository.

    My solution

    My solution is to use git add . after git init command.

    Use the following set of commands in the same order to overcome the problem:

    git init
    git add .
    git commit -m "first commit"
    git branch -M main
    git remote add origin "_git repository link here_"
    git push -u origin main
                    This git branch -M main is important which is why it wasn't working for me. It expects master if you don't put anything.
    – Panama Jack
                    Mar 21, 2022 at 0:22
    

    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 the branch in 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
                    Everything above didn't help, but finally this did. It can be many different issues with the same error message.
    – Jesse
                    Jun 10, 2022 at 5:42
    

    Two possibilities :

    1- Either you forgot to include the .gitignore file.

    Here are all the steps required:

  • Create an empty Git repository on remote,

  • On local create the .gitignore file for your project. GitHub gives you a list of examples here

  • 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
    

    2- Or you are trying to create a new Github project.

    Github replaced master with main as the default branch name. To resolve the issue :

  • On your local project:
  • remove the .git folder if it exists
  • recreate a clean repository by launching the following in your project:
  • in the terminal:

    git init
    git add .
    git commit -m "YOUR FIRST MESSAGE HERE"
    git branch -M main
    git remote add origin _GIT_LINK_TO_PROJECT_HERE_
    git push -u origin main
                    How is this the error in question related to .gitignore?! If it is related (which I highly doubt) you should explain it in your answer. Thanks
    – pedram bashiri
                    Jan 6, 2020 at 0:11
    

    After the GitHub update 01.10.20 you should use main instead of master.

    Do it like these way...

  • Create a repository on GitHub
  • Delete existing .git file on your local directory
  • Go to local project directory and type git init
  • git add .
  • git commit -m"My First Commmit"
  • Now check your branch name it will be master in your local project
  • git remote add origin <remote repository URL past here from the github repository> then type git remote -v
  • git push -f origin master
  • Now check the github repository you will see two branch 1. main 2. master
  • In your local repository create new branch and the branch name will be main
  • git checkout main
  • git merge master
  • git pull origin main
  • git push -f origin main
  • Note: from 01.10.20 github decided use main instead of master branch use default branch name

    The original announcement from GH CEO Nat Friedman on Twitter in response to the #BLM Movement. twitter.com/natfriedman/status/1271253144442253312 – ultrasounder Nov 27, 2020 at 17:39

    I have faced the same issue, and this solved my problem:

    Just make a branch:

    git checkout -b "master"

    After that,

    git push -u origin master

    Boom.

    I got the same issue and realized that branch was on main. then I followed this and got resolved. – Harish Nov 3, 2021 at 23:07 This was my problem. Tried so many things and then realized it was main instead of master. – Balasubramanian S Sep 7, 2021 at 8:12

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

  •