3997

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'
aloisdg
  • 22,270
  • 6
  • 85
  • 105
sinoohe
  • 40,334
  • 3
  • 19
  • 16
  • 68
    @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 08 '13 at 00:42
  • Did you set up your git config commands to install and configure git globally on your machine? – IgorGanapolsky Nov 04 '13 at 01:18
  • 4
    Hope this post would be useful to somebody- http://samranga.blogspot.com/2015/07/create-git-bitbucket-repository-from.html?view=sidebar 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 02 '15 at 13:00
  • 1
    I received this error trying to push the wrong branch name. Resolved using `git status` to get the proper one. – Tom Howard Sep 12 '15 at 10:33
  • Adding a comment to call out @aug2uag's alternative answer - sleepily skipping `git commit` can cause this error, as well! – Jaime Jan 07 '16 at 01:08
  • 44
    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 '17 at 09:28
  • 11
    If you didnt add `git add` with dot or some files this error also will appear. – Blasanka Apr 28 '18 at 10:18
  • 1
    The above error can come up when you have an incorrect branch name, so for others facing the same issue it would be helpful to double check that. – Persistent Plants Jul 19 '18 at 01:06
  • Here http://note.yuhc.me/2015/01/git-push-error-refspec-not-match/ I found the ebst answer. If you just cloned the repo and it is not empty then you can push by specifying that the branch is in the HEAD like this $ git push origin HEAD: – lesolorzanov Jan 18 '19 at 16:05
  • check your privilege in my case i need to check my permission i have two private git repositories and this second account is admin of that new repo and first one is my default user account and i should grant permission to first – saber tabatabaee yazdi Feb 06 '20 at 16:35
  • Or you may wrote non-existence branch. – M Y May 18 '20 at 16:05
  • FWIW I got this error when I tried to push to an uninitialized remote repo with no local commits done. Doing initial local commit then pushing worked. – Y00P Jun 21 '20 at 07:00
  • 32
    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 '20 at 04:39
  • will you honestly just be corteous enough to accept an answer? – SaginiChan Apr 25 '21 at 12:16
  • @sinoohe, if you have only main branch, try "git push origin HEAD :main " It worked for me. – Vikranth Inti May 12 '21 at 11:12
  • Is an initial commit missing? –  Jul 19 '22 at 09:35
  • Now 142 answers (incl. deleted ones) – Peter Mortensen Sep 18 '22 at 17:56
  • by default, your branch name is "master" in your Git but GitHub's default branch name is main, you can change your branch name by the following command `git branch -M main` and it will work – Sami Dec 30 '22 at 22:58

137 Answers137

5670

Maybe you just need to commit. I ran into this when I did:

mkdir repo && cd repo
git init
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 main

Success!

ChrisB
  • 1,540
  • 6
  • 20
baisong
  • 57,052
  • 1
  • 15
  • 7
  • 183
    Don't just follow this step blindly, look at what @Vi has mentioned, and then modify your push command to correct ref. – Kumar Jun 07 '12 at 16:43
  • 1
    Damn, happened to me as well. Been using XCode for Git for too long and took it for granted that you need to commit before pushing. – Ben Mar 27 '13 at 18:57
  • 79
    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 '15 at 07:57
  • 6
    Fixes different issue which is nice but not really an answer to _This_ actual question. – Michael Durrant Jun 08 '15 at 00:14
  • Hope this post would be useful to somebody- http://samranga.blogspot.com/2015/07/create-git-bitbucket-repository-from.html?view=sidebar 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 02 '15 at 12:59
  • Also happens if you type the name of your branch incorrectly. Typos. – Jen Nov 11 '15 at 08:50
  • 9
    `git commit -m 'initial commit'` should be double quoted. '`git commit -m "initial commit"`, at least on windows. – dance2die Jun 11 '16 at 13:12
  • there has to be something to commit, you get no error if you commit an empty directory but then this error occurs when you try to push – Arno Feb 05 '17 at 12:05
  • `nothing added to commit but untracked files present Monas-MacBook-Pro:02_02 mona$ git push origin master error: src refspec master does not match any. error: failed to push some refs to 'https://github.com/monajalal/jupyter_notebooks.git'` didn't work for me! – Mona Jalal Jun 15 '17 at 05:06
  • My problem was I was misspelling the name of my local branch when pushing. I was doing `git push scheduler` when my local branch was named **sheduler**. Once I fixed the local name to say s**c**heduler it works. HAHA :) – protoEvangelion Jul 17 '17 at 17:13
  • 44
    Another possible reason: you don't actually have a branch called *master* – CarlosAS Feb 16 '18 at 19:14
  • Not sure if I missed it in the fine print but an important point is you need to add a file to a new repo before committing. – Ashley Duncan Jun 21 '18 at 03:10
  • @baisong this does not work if I `git remote set-url origin ` and try to `git add .` , `git commit -m "intial commit'`, `git push origin master` – mLstudent33 Apr 19 '19 at 09:17
  • Oops! Never committed! section worked for me , i had even committed but there was some issue that's why it make error for me – Usman Ali Maan Apr 22 '19 at 21:51
  • 1
    This is what happens when you follow BitBucket's (incomplete) instructions for setting up your repo. Lol. – Andrew Koster Jul 03 '19 at 23:05
  • Any chance to avoid this for Heroku? It is annoying when you often create projects to be forced to push some random file, especially that if you set-up build, heroku will reject the push. For every new project my pipeline breaks if I won't pull using cli and push. Tried `git push -f heroku master:refs/heads/master` and `git push -f heroku HEAD:master` - both don't work for the first time... – Greg Wozniak Jul 13 '19 at 09:02
  • This also might occur if you have not added any files in the staging area. You can run `git status` to check which files have you staged. – Kewal Shah Sep 27 '19 at 11:41
  • Or you may wrote non-existence branch. – M Y May 18 '20 at 16:04
  • 1
    I forgot to commit then ran to this. **JUST COMMIT** – Napster Scofield Sep 20 '20 at 08:05
  • 1
    As CarlosAS has pointed out, you can get this error if there's no branch "master", and that's actually a common case right now, because as of June 2020 github has stopped using branches named "master" in favor of "main". – Joseph Brenner Oct 10 '20 at 02:16
  • 1
    github rename the master with main. so you can use git remote origin main – saber tabatabaee yazdi Nov 05 '20 at 18:23
  • 2
    I ran into this and the problem was I hadn't set my branch `git branch -M ` – Kinyugo Feb 21 '21 at 15:30
  • I did commit, but when I try to push my commit to the main branch, which is now the default branch in GitHub, I get this error. If I push to master, there is no error, but then I have two branches in my GitHub! – Mehdi Abbassi Mar 05 '21 at 09:39
  • giving initial commit solved my problem: git commit -a -m"commit message" – Rafiq Mar 28 '21 at 09:04
  • 1
    `master` instead of `main` solve me problem – zac Apr 25 '21 at 17:44
  • I tried your method but still getting error: src refspec master does not match any – Karen Goh May 07 '21 at 04:25
  • I also recommand watching this tutorial : https://youtu.be/yZdmcMQkQRo – iifast2 Aug 17 '21 at 10:53
  • Github now changed ```git push origin master``` to ```git push origin main``` – Vintage Coder Sep 21 '21 at 15:02
  • An other reason is that you have no complete repo, but only a shallow one. (i.e., you cloned with --depth=1 flag or something similar) – fiorentinoing Oct 11 '21 at 08:35
  • I had the very same thing happen to me. I created a new local git repo, using `git init`, and then tried to make the first commit. This was met with an error, and git told me to tell it who I am. After setting the user.email and user.name, I simply forgot to do the commit; and voila when I tried to push to a new, empty repo on GitHub, this very error occured. – Dohn Joe Feb 07 '23 at 15:06
1739
  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).
Vi.
  • 37,014
  • 18
  • 93
  • 148
  • 7
    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 '10 at 04:26
  • 12
    git checkout -b testbranch ; git push origin testbranch:master – sinoohe Nov 17 '10 at 04:27
  • 171
    `git show-ref` showed my branch; the `git push origin HEAD:` worked for me. – Glen Solsberry Nov 25 '11 at 22:01
  • 14
    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 '12 at 14:09
  • 5
    @gms8994 -- you're correct. if someone creates a branch as a first thing instead of pushing into master, the same `unfriendly` error shows up. use `git push origin ` instead of `master` in case you attempt to `git checkout -b ` before any FIRST push after initial `git remote add origin ` – asyncwait Jul 06 '13 at 20:33
  • @Vi. It worked for me, Thanks a lot.. But everytime i have to use the same command.. else showing the same error when trying to commit by git push origin master .. Please help me to overcome this.. – Mr.Chowdary May 22 '14 at 05:43
  • 3
    @Mr.Chowdary, You can configure the "default push location" of the branch, like in `git push --set-upstream origin refs/heads/the_remote_branch`. Also you can create a script or alias that will do `git push origin HEAD:/refs/heads/master` with little typing. – Vi. May 22 '14 at 11:17
  • What if no refs/heads/master found ?? – Sunil Sharma Jan 16 '16 at 08:56
  • @SunilSharma, Then use some ref that is found, or explicit hex commit-id. – Vi. Jan 16 '16 at 11:01
  • This seems to only work if I force it .. `git push origin HEAD:master --force`, but I still can't do a normal `git push` . I'm curious what I need to do to get past this original error. – Trip Mar 09 '16 at 12:33
  • @Trip, Note that there is also option `--force-with-lease`, which is safer that usual `--force`, especially when you use the repository not alone. – Vi. Mar 09 '16 at 15:04
  • 1
    Thank you @GlenSolsberry - The `HEAD:` prefix was exactly what i needed for this problem. i have local branches that look like `dev/something-or-other` and the remote is just `dev`. Apparently git was confused about the path-like branch names, so `git push HEAD:dev` worked perfectly. – cautionbug Jul 19 '17 at 21:12
  • This happened to me when trying to psh a tag that didn't exist. – c4sh Dec 04 '18 at 16:18
  • 2
    If this answer results in no results (just blank..) then if you have the same case as me, you may have just forgot to 'add' any files. So I did: 'git add --all', then 'git commit -m "initial commit"', then 'git push origin HEAD:master' to resolve. – Chris Dec 14 '18 at 15:48
  • 1
    I wanted to push changes in new branch instead of master. I just changed the above mentioned code with `git push -u origin HEAD:feature/random_name` and that worked for me. – Rahul Mishra Mar 28 '19 at 05:06
  • for me it was unnecessary single quote in the branch name 'cmd-line-runnable' .. that I must have put by mistake. `git show-ref` helped to understand it. – old-monk Apr 11 '19 at 21:34
  • 20
    `master` is changed to `main` now. – Aaditya Ura Oct 16 '20 at 11:02
  • git --version 2.29.1.windows.1 switched from "master" to "main". My GOGS server's templates still use "master". After initializing my repository locally, all I need to do is `git push -u origin main` instead of `git push -u origin master` – gridtrak Nov 23 '20 at 14:36
  • putting the HEAD: before the remote branch name worked like a magic. – yasnil Jul 09 '21 at 03:11
  • I did not get a Gerrit url to indicate the patch was updated but it was! – boardtc Sep 21 '21 at 07:27
  • git push origin HEAD:master --force. ... add (--force) if not working – Suhad Bin Zubair Oct 29 '22 at 12:54
  • `git push origin HEAD:master` solved my issue. Then if you want to use `master` branch then you can switch to `master` branch by `git checkout master` command. Thanks – Kamlesh Dec 08 '22 at 17:09
281

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aryo
  • 4,349
  • 2
  • 28
  • 24
  • 18
    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 05 '13 at 01:35
  • See [this earlier answer](http://stackoverflow.com/a/7543112/456814). I suspect that you needed to add a file because git won't track empty directories. –  Apr 04 '14 at 20:57
  • 4
    push --force could also completely blew away co-workers hard work. Added warning by it. – Michael Durrant Jun 08 '15 at 00:15
  • 4
    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 04 '15 at 13:36
151
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
139

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
grg
  • 5,023
  • 3
  • 34
  • 50
pyfork
  • 3,747
  • 2
  • 21
  • 18
122

This happened to me in a brand new repository after I ran git add with only an empty directory.

As soon as I added a file (e.g. a git add README.md), then git push worked great.

Andrew E
  • 7,697
  • 3
  • 42
  • 38
  • 9
    This probably works because git doesn't actually track directories, only files. So if a directory is empty, git won't actually add it. –  Apr 04 '14 at 20:51
  • 1
    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 08 '15 at 00:21
  • 1
    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 '15 at 14:14
  • 1
    8 years later this saved me some headache! – rvictordelta Oct 26 '19 at 17:25
  • 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 03 '19 at 09:14
  • @rao yep, adding an origin isn't enough, you must add and commit files. But why do you have "git git push..."? Is the second "git" a typo? – Andrew E Dec 03 '19 at 10:44
  • Yes it's a typo. Thank you – Kodali444 Dec 12 '19 at 09:52
81

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
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
aug2uag
  • 3,379
  • 3
  • 32
  • 53
73

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

git init
git add .
git commit -m 'message'
git push -u origin master
Werner
  • 2,537
  • 1
  • 26
  • 38
pratik kumar
  • 831
  • 6
  • 4
67

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'
tanius
  • 14,003
  • 3
  • 51
  • 63
wilsonfoz
  • 867
  • 8
  • 9
  • 4
    LOL. I was trying to push to origin master but that branch didn't exist. It was called origin stable. – penner May 27 '13 at 23:35
  • 1
    The `-u` may have helped here. – Michael Durrant Jun 08 '15 at 00:22
  • 3
    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 '16 at 00:01
  • 4
    I just got this when I misspelled the branch name. – Sebastian Ärleryd May 02 '17 at 14:58
  • 2
    My local branch was spelled "s**h**eduler" and I was doing `git push origin scheduler`. HA! One letter off will kill you in programming. lol – protoEvangelion Jul 17 '17 at 17:11
  • Exactly. This was the issue for me. I was trying to push to a `dev` branch from my local that does not exist yet on my local. I was in the `main` branch, so I had to do `git checkout -b dev` and then `git push origin dev`. Thank you. – Promise Preston Sep 18 '21 at 07:30
  • this saves my day!!! I mixed it up with `main` and `master` – Hui-Yu Lee Nov 11 '21 at 00:54
66

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 the README.md file and GitHub instructs us with basic commands while creating the repository.

My solution

My solution is to use git add . after the 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aswin Barath
  • 889
  • 9
  • 13
65

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.

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
63

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
Saurabh Singh
  • 1,241
  • 13
  • 11
61

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arslan Ahmad khan
  • 5,426
  • 1
  • 27
  • 33
  • 1
    Everything above didn't help, but finally this did. It can be many different issues with the same error message. – Jesse Jun 10 '22 at 05:42
46

Two possibilities:

1- Either you 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
    

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:

  1. On your local project:
    1. remove the .git folder if it exists
    2. 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ismail H
  • 4,226
  • 2
  • 38
  • 61
43

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.

bleistift2
  • 494
  • 5
  • 14
Areeha
  • 823
  • 7
  • 11
33

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

Lucas
  • 523
  • 2
  • 10
  • 20
Sumer
  • 2,687
  • 24
  • 24
30

After the GitHub update 2000-10-01, you should use main instead of master.

Do it like this way...

  1. Create a repository on GitHub
  2. Delete existing .git file in your local directory
  3. Go to the local project directory and type git init
  4. git add .
  5. git commit -m"My first commit"
  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>, and 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 a 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 2020-10-01, GitHub decided use main instead of master branch to use as the default branch name.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iamtheasad
  • 1,017
  • 13
  • 15
  • The original announcement from GH CEO Nat Friedman on Twitter in response to the #BLM Movement. https://twitter.com/natfriedman/status/1271253144442253312 – ultrasounder Nov 27 '20 at 17:39
28

Just add an initial commit. Follow these steps:

  • git add .

  • git commit -m "initial commit"

  • git push origin master

This worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NeeruKSingh
  • 1,545
  • 3
  • 22
  • 26
28

Feb, 2022 Update:

If your branch is "main":

enter image description here

Run this command:

git push origin main

If your branch is "master":

enter image description here

Run this command:

git push origin master
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
27

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.

Anthony
  • 13,434
  • 14
  • 60
  • 80
27

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alamin
  • 1,878
  • 1
  • 14
  • 34
26

Maybe the branch is main instead of master.

Try

git push origin HEAD:main

or

git push origin main

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sankalp Gour
  • 271
  • 4
  • 5
23

GitHub changed the default branch name from master to main. So if you created the repository recently, try pushing the main branch:

git push origin main

Reference

Renaming the default branch from master (GitHub)

shivampip
  • 2,004
  • 19
  • 19
21
  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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alwan Mortada
  • 278
  • 3
  • 10
21

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
snap
  • 1,598
  • 1
  • 14
  • 21
21

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

git commit -m "first commit"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Badr Bellaj
  • 11,560
  • 2
  • 43
  • 44
21

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dinith
  • 839
  • 14
  • 22
21

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

user993563
  • 18,601
  • 10
  • 42
  • 55
20

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

git add .
git commit -m 'initial commit'
git push origin master
xuri
  • 840
  • 7
  • 18
20

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
neoDev
  • 2,879
  • 3
  • 33
  • 66
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gel
  • 2,866
  • 2
  • 18
  • 25
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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Deekshith Anand
  • 2,175
  • 1
  • 21
  • 24
17

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aayush Neupane
  • 1,066
  • 1
  • 12
  • 29
  • [Another answer](https://stackoverflow.com/questions/4181861/message-src-refspec-master-does-not-match-any-when-pushing-commits-in-git/66571513#66571513) claims at the end of 2020. – Peter Mortensen Sep 18 '22 at 18:18
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
d.raev
  • 9,216
  • 8
  • 58
  • 79
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3051460
  • 1,455
  • 22
  • 58
13

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

Gavin
  • 7,544
  • 4
  • 52
  • 72
  • 3
    I suspect a great many of us who came here via web search actually mistyped the name! – sage Jun 23 '14 at 02:49
13
git add .

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HuntsMan
  • 762
  • 5
  • 16
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Govind
  • 2,482
  • 1
  • 28
  • 40
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I gave a similar answer that helped me on this question: https://stackoverflow.com/a/66737426/5996491 – Kale Mar 21 '21 at 20:55
11

You need to configure your Git installation if it is the first time that you use it, with:

git config --global user.email "you@example.com"

git config --global user.name "Your Name"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
asdasd
  • 127
  • 1
  • 2
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rabbitwerks
  • 285
  • 1
  • 3
  • 9
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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
Shimanto
  • 283
  • 5
  • 11
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lackeeee
  • 459
  • 1
  • 8
  • 13
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
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

8

Try this:

git add .

git commit -m "your commit message"

git remote add origin *remote repository URL*

git push origin *your-branch-name*
Pramesh Bajracharya
  • 2,153
  • 3
  • 28
  • 54
Palak Jain
  • 664
  • 6
  • 19
8

Only commit solved this error:

git commit -m "first commit"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Omid Ahmadyani
  • 1,430
  • 13
  • 15
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vishal
  • 99
  • 1
  • 5
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hamdhan Azeez T
  • 429
  • 4
  • 9
7

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

git checkout master
git commit -a -m "your comment"
git push origin master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Giulio Roggero
  • 1,692
  • 1
  • 16
  • 11
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>>.

Alkanshel
  • 4,198
  • 1
  • 35
  • 54
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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
c__c
  • 1,574
  • 1
  • 19
  • 39
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
Shivam Jha
  • 3,160
  • 3
  • 22
  • 36
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anubhav Pandey
  • 1,285
  • 1
  • 14
  • 18
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
Vishal Verma
  • 163
  • 2
  • 2
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Almuntasir Abir
  • 320
  • 1
  • 5
  • 16
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arya Mohanan
  • 577
  • 5
  • 8
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/29909491) – east1000 Sep 24 '21 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 – Arya Mohanan Sep 25 '21 at 15:42
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
jcw
  • 5,132
  • 7
  • 45
  • 54
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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ferhat KOÇER
  • 3,890
  • 1
  • 26
  • 26
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RileyManda
  • 2,536
  • 25
  • 30
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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nz_21
  • 6,140
  • 7
  • 34
  • 80
  • It was 92 answers (incl. deleted). – Peter Mortensen Sep 18 '22 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. – Peter Mortensen Sep 18 '22 at 18:05
  • Another answer indicates the origin of the change [is on GitHub](https://stackoverflow.com/questions/4181861/message-src-refspec-master-does-not-match-any-when-pushing-commits-in-git/65329319#65329319). – Peter Mortensen Sep 18 '22 at 18:10
6

As one of the options for solving your problem:

git push origin HEAD
gorevanova
  • 539
  • 1
  • 7
  • 16
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zhengming Ying
  • 447
  • 4
  • 6
5

I had a similar error. But Git tells me:

*** Please tell me who you are.

Run

git config --global user.email "you@example.com"
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paket2001
  • 420
  • 1
  • 6
  • 19
  • I migrated to a new machine and was getting these errors. Setting my GIT info fixed it. – swt83 Jan 05 '16 at 16:15
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
Ishrak
  • 509
  • 1
  • 9
  • 17
5

Maybe GitHub doesn't know who you are.

First you have to run:

git config --global user.email "you@example.com"
git config --global user.name "Your Name"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amir.S
  • 719
  • 8
  • 15
5

Try git show-ref

You might see refs/heads/live

This means you should do

git push -u origin live
jflaga
  • 4,610
  • 2
  • 24
  • 20
5

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Napster Scofield
  • 1,271
  • 14
  • 13
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.

Ayush Jain
  • 316
  • 4
  • 11
  • 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 – Hileamlak Yitayew Oct 27 '20 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 '20 at 21:03
5

For Repositories WIKI, I encountered this error also.

git show-branch

if shows master then

git push -u origin master
jian
  • 4,119
  • 1
  • 17
  • 32
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
espradley
  • 2,138
  • 2
  • 17
  • 15
  • 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. –  Jun 17 '14 at 16:59
4

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gujarat Santana
  • 9,854
  • 17
  • 53
  • 75
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali
  • 181
  • 1
  • 3
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".

Sina Madani
  • 1,246
  • 3
  • 15
  • 27
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.

AzafoCossa
  • 874
  • 8
  • 18
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 ...

kutukmu
  • 93
  • 7
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/64350104/edit) (it covers answers as well). Thanks in advance. – Peter Mortensen May 18 '23 at 20:49
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gemfield
  • 3,228
  • 7
  • 27
  • 28
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SaltyCatFish
  • 59
  • 1
  • 3
  • 1
    That is valid only for new repositories. So this answer is completely inaccurate. More info here: https://github.com/github/renaming – zinovyev Oct 23 '20 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. – SaltyCatFish Oct 25 '20 at 20:08
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shyam
  • 537
  • 6
  • 16
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pramod
  • 806
  • 1
  • 8
  • 10
  • 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 `? –  Apr 04 '14 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vojtech Vitek - golang.cz
  • 25,275
  • 4
  • 34
  • 40
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
equivalent8
  • 13,754
  • 8
  • 81
  • 109
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Janac Meena
  • 3,203
  • 35
  • 32
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
discipleartem
  • 181
  • 12
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sohail
  • 587
  • 7
  • 25
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 "you@example.com"
 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
samezedi
  • 621
  • 5
  • 15
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
3

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/username/reponame.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.

Vishnu
  • 124
  • 2
  • 12
  • I see that a few people are downvoting this answer. Just to be clear, I came across this question when I got the same error when pushing my changes to the master branch. On further reading, I got to know that GitHub has changed master to main and I just wanted people to know about it and that was the reason for this answer. – Vishnu Oct 08 '20 at 11:58
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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

Initially, every file is in an untracked state. We need to add untracked files from the working directory to the staging area, and only then will Git start tracking the files. From the working directory, we move files to the staging area which are ready for the next commit, so that Git is prepared to take a snapshot of them to create a new version. You skipped the staging area and added only one file to the staging area.

First go to your local folder which you want to push and then follow these steps.

git init

git add .

git commit -m "First commit"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
psygo
  • 95
  • 2
  • 10
3

I was on the wrong branch. Check your branch. I was on a 'master' branch instead of 'main'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32101735) – YesThatIsMyName Jul 01 '22 at 08:24
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!!

2

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cherry
  • 336
  • 1
  • 2
  • 9
2

Check that the current branch is master only. I faced the same issue and my branch was main. So running the following command did the work for me:

git push origin main
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

I encountered this issue and the root cause was I followed the steps as is mentioned in GitLab upon creating a new project

  • cd existing_folder
  • git init --initial-branch=main
  • git remote add origin yourrepo
  • git add .
  • git commit -m "Initial commit"
  • git push -u origin main

without adding any file in the local directory before the push therefore it wasn't considered as commit because there was nothing to commit ( though you run git add . and git commit -m "initial commit" commands. Make sure you add some files in your local directory then only git will consider it as commit and then pushing resolved the error

Milan Kumar
  • 361
  • 1
  • 6
  • 16
2
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

Use above commands you might be missing "git branch -M main" (this particular command)

bfontaine
  • 18,169
  • 13
  • 73
  • 107
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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aimango
  • 1,567
  • 2
  • 15
  • 29
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
arush436
  • 1,748
  • 20
  • 20
1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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.

kamilk
  • 3,829
  • 1
  • 27
  • 40
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.

M. Dhaouadi
  • 617
  • 10
  • 27
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
oskarko
  • 3,382
  • 1
  • 26
  • 26
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Exil
  • 311
  • 2
  • 11
  • 26
1

Permissions issue? Resolution: fork it

I got this error because I didn't have any 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MAX
  • 1,562
  • 4
  • 17
  • 25
1

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

cSn
  • 2,796
  • 3
  • 23
  • 29
1

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 'name@mail.com'

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Otee
  • 90
  • 4
1
  1. git branch

  2. Rename the branch with the -mv flag:

    git branch -mv origin master

After this, git branch should show master.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Safaetul Ahasan Piyas
  • 1,285
  • 1
  • 8
  • 10
  • The purpose of `git branch` ought to be explained. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/66503119/edit), not here in comments (but *** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 18 '23 at 21:12
  • [Ivan Jovović's answer](https://stackoverflow.com/questions/4181861/message-src-refspec-master-does-not-match-any-when-pushing-commits-in-git/66563191#66563191) has a clue. – Peter Mortensen May 18 '23 at 21:15
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ivan Jovović
  • 5,238
  • 3
  • 29
  • 57
  • U+0096 is [listed as](https://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=0x) . What is the most likely equivalent? ASCII 0x16 ([SYNC / SYN](https://en.wikipedia.org/wiki/ASCII#Control_code_chart)? AKA Ctrl + V (presumably in a terminal context). It would not be uncommon to use Ctrl + V in a terminal window). – Peter Mortensen May 18 '23 at 21:20
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.

Santosh Pillai
  • 8,169
  • 1
  • 31
  • 27
1
git init

First command first... First, we need to initialize // and this should work for sure!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bharath Kumar
  • 893
  • 9
  • 8
1
git init
git add .
git commit -m "message"
prompt me to enter email and user name
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

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

git push
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ram
  • 91
  • 4
1

In my case, the error occurred when I was pushing changes to the branch which I only had locally. The remote branch did not exist with the same name. I resolved the error with the following command:

git push --set-upstream origin "your-branch-name"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Guram Khasia
  • 114
  • 4
1

I think the problem stems from adding and committing your node_modules folder.

When you use git add ., you may get that infinite scroll as if it is adding several files. If so, you are not only adding your scripts but also the node_modules folder which is large in size.

The best solution is to create a .gitignore file, inside of which you paste that # dependencies /node_modules.

Now when you add your files, Git will ignore the node_modules folder.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
uki
  • 47
  • 3
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!

DaVinziBoi
  • 33
  • 9
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

Manishyadav
  • 1,361
  • 1
  • 8
  • 26
1

For me, it seemed to be because I had already done the commit process, so I got this after trying to commit again.

Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Since I saw that the path was origin/main, I pushed to main instead of master and it worked.

NB: Check to ascertain if the primary branch is main or master.

As of 2022.

1
 sudo git push -v --progress "origin" : origin/prod_18aug2022

This worked for me when pushing from a local to remote branch.

Himanshu Saini
  • 702
  • 11
  • 25
1

My branch was named bugfix-22112022/5v8ST5Q6/181-kontaktformular-eingabe-nach-anmeldung-übernehmen, <-- yes it had a comma in it and I thought the comma is not part of it and deleted it when trying to push...

I renamed my branch:

git branch -m bugfix-22112022/5v8ST5Q6/181-kontaktformular-eingabe-nach-anmeldung-übernehmen
Black
  • 18,150
  • 39
  • 158
  • 271
1

There are mainly 2 possibilities for this question. Let us consider this one by one.

  1. It can be possibly be the error that, master branch does not even exist. In my case, it was named main, and NOT master. In this case, you need to replace master with main.

  2. You haven't even committed your changes. If this is the case, then go for -->

  • git add .

  • git commit -m "initial commit"

  • git push origin <branch>

Then it should work.

Try and it will be good. :)

1

This issue can be solved by re-initializing git on the project folder

#Type the following on the CMD
git init

#Then try pushing your code online
git push
1

You may encounter this problem when any of your folder is already linked to other git repository. Visit the folder that is not being added and delete the .git file (if you donot want it to link to that repo any more) from there and try adding and commiting again

1

I fixed this issue with using this command before commit and it worked:

git add .    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amin Rezaew
  • 298
  • 1
  • 14
  • That is more or less identical to [baisong's answer](https://stackoverflow.com/a/7572252), [aug2uag's answer](https://stackoverflow.com/a/13213212), [xuri's answer](https://stackoverflow.com/a/23457277), [pratik kumar's answer](https://stackoverflow.com/a/27907592), [Gujarat Santana's answer](https://stackoverflow.com/a/29679106), and [Alwan Mortada's answer](https://stackoverflow.com/a/30783385). That is just from the first 30 answers. – Peter Mortensen May 18 '23 at 15:30
  • It is identical to [HuntsMan's answer](https://stackoverflow.com/questions/4181861/message-src-refspec-master-does-not-match-any-when-pushing-commits-in-git/39784374#39784374) and [neoDev answer](https://stackoverflow.com/questions/4181861/message-src-refspec-master-does-not-match-any-when-pushing-commits-in-git/42181635#42181635). – Peter Mortensen May 18 '23 at 15:34
  • And [Sumer's answer](https://stackoverflow.com/questions/4181861/message-src-refspec-master-does-not-match-any-when-pushing-commits-in-git/55851884#55851884) – Peter Mortensen May 18 '23 at 15:41
  • Related: *[Why do I need 50 reputation to comment? What can I do instead?](https://meta.stackexchange.com/questions/214173/)*. – Peter Mortensen May 18 '23 at 15:46
  • @PeterMortensen Yes I know they answered before me but I shared my own experience that fixed my issue. – Amin Rezaew May 19 '23 at 09:48
1

My problem was that I did not switch to the main branch before making a commit

git branch -M main
Kermit
  • 4,922
  • 4
  • 42
  • 74
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
barfuin
  • 16,865
  • 10
  • 85
  • 132
0

I had an extremely long local branch name.

git branch -m new_shorter_branch_name

fixed the problem.

Scotty.NET
  • 12,533
  • 4
  • 42
  • 51
0

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
blackjacx
  • 9,011
  • 7
  • 45
  • 56
  • What is *"fastlane match nuke development"*, etc.? Some kind of weird Git client? – Peter Mortensen Nov 18 '19 at 00:35
  • Nope, in fact `fastlane match nuke development` deletes the development certificates and profiles from the git credential store and the Developer Portal (I edited my answer). – blackjacx Nov 19 '19 at 10:12
0

Change master to main. That should work.

git push origin main
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hacker
  • 37
  • 12
  • 1
    Thank you for your interest in contributing to the Stack Overflow community. This answer already has **many** answers—including several that have been extensively validated by the community. Are you _certain_ your approach hasn’t been given previously? **If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient.** Can you kindly [edit] your answer to offer an explanation? – Jeremy Caney May 09 '23 at 07:12
  • 1
    E.g., https://stackoverflow.com/a/72344753/3025856. But also, this question isn’t tagged with GitHub and, regardless, was published long before GutHub changed the default branch to main. – Jeremy Caney May 09 '23 at 07:14
0

This is 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 master branch but github has moved from master to main here

To solve this issue: Use git checkout -b main to switch to main branch Use git pull origin main to pull the main branch, 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.

-1

I post this answer because it really confused me and i had to dig some details.

"git init" resulted in master branch being created as top branch. Github's default top branch has been renamed from "master" to "main", due to a hilarious discussion about words... More here incl. the solution more here

TLDR: To solve the problem I had to rename my local top branch from "master" to "main"... --> git branch -m master main

These sort of things holding people back from serious coding work, but I guess ultimately word do matter ;)

Coni
  • 45
  • 2
  • This solution has already been given (https://stackoverflow.com/a/76608673/341994). There's no merit in giving it again, and the rest of your answer is really just a rant. — Note that if your `git init` is giving you `master` as the default unborn branch, you need to update your Git, because modern Git uses `main` just like GitHub. — Final comment, read my https://www.biteinteractive.com/of-git-and-github-master-and-main/ which tells you more about this problem than you ever ever ever wanted to know. – matt Aug 03 '23 at 15:41
-2

I had the same error; this is how mine was solved:

git config --global user.email "YOUR EMAIL"
git config --global user.name "YOUR NAME"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SMMH
  • 310
  • 1
  • 13