3640

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
  • 19,231
  • 5
  • 81
  • 97
sinoohe
  • 36,756
  • 3
  • 17
  • 16
  • 60
    @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
  • 3
    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
  • 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
  • 32
    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
  • 6
    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
  • 22
    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

123 Answers123

5181

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

mkdir repo && cd repo
git remote add origin /path/to/origin.git
git add .

Oops! Never committed!

git push -u origin master
error: src refspec master does not match any.

All I had to do was:

git commit -m "initial commit"
git push origin main

Success!

Coder
  • 1,134
  • 1
  • 11
  • 25
baisong
  • 52,136
  • 1
  • 13
  • 6
  • 172
    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
  • 67
    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. – Jennifer Michelle 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
  • 37
    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
  • `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 – iifast 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
1559
  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.
  • 33,936
  • 16
  • 92
  • 141
  • 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
  • 155
    `git show-ref` showed my branch; the `git push origin HEAD:` worked for me. – Glen Solsberry Nov 25 '11 at 22:01
  • 10
    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
  • 17
    `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
259

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,030
  • 21
  • 100
  • 124
Aryo
  • 4,109
  • 2
  • 27
  • 23
  • 17
    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
138
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,030
  • 21
  • 100
  • 124
VIKAS KOHLI
  • 7,314
  • 3
  • 48
  • 49
132

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
  • 4,278
  • 3
  • 31
  • 43
Red
  • 3,348
  • 2
  • 19
  • 18
108

I found this happened in a brand new repository after I Git added only a directory.

As soon as I added a file (e.g. a README), Git push worked great.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Andrew E
  • 6,799
  • 3
  • 34
  • 35
  • 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
75

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
  • 135,913
  • 89
  • 401
  • 325
aug2uag
  • 3,221
  • 2
  • 28
  • 51
69

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,507
  • 1
  • 25
  • 38
pratik kumar
  • 791
  • 5
  • 4
59

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
  • 10,120
  • 3
  • 43
  • 51
wilsonfoz
  • 787
  • 7
  • 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
  • 2
    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` – iownthegame Nov 11 '21 at 00:54
57

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,143
  • 11
  • 11
57

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
  • 18,308
  • 12
  • 99
  • 160
50

I faced the same issue some days ago.

If you created a new repository nowadays(2020) then the default branch is main on GitHub.

you can check on GitHub now in your repository branches.

and you can also check branch on the terminal by running the command:

git branch

so that's why you need to run

git push origin main

instead of

git push origin master

Goodluck

Arslan Ahmad khan
  • 4,714
  • 1
  • 23
  • 31
48

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
Aswin Barath
  • 629
  • 6
  • 11
43

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
Ismail H
  • 3,734
  • 1
  • 30
  • 56
41

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
  • 450
  • 5
  • 13
Areeha
  • 803
  • 7
  • 11
32

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

Lucas
  • 456
  • 2
  • 11
  • 15
Sumer
  • 2,388
  • 20
  • 21
29

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

Do it like these way...

  1. Create a repository on GitHub
  2. Delete existing .git file on your local directory
  3. Go to local project directory and type git init
  4. git add .
  5. git commit -m"My First Commmit"
  6. Now check your branch name it will be master in your local project
  7. git remote add origin <remote repository URL past here from the github repository> then type git remote -v
  8. git push -f origin master
  9. Now check the github repository you will see two branch 1. main 2. master
  10. In your local repository create new branch and the branch name will be main
  11. git checkout main
  12. git merge master
  13. git pull origin main
  14. git push -f origin main

Note: from 01.10.20 github decided use main instead of master branch use default branch name

iamtheasad
  • 749
  • 8
  • 12
  • 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
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
  • 12,124
  • 13
  • 54
  • 68
27

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,030
  • 21
  • 100
  • 124
NeeruKSingh
  • 1,479
  • 3
  • 20
  • 25
24

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
Kai - Kazuya Ito
  • 6,454
  • 5
  • 44
  • 50
24

Maybe the branch is main instead of master

try

git push origin HEAD:main or git push origin main

Sankalp Gour
  • 251
  • 3
  • 5
21

I have faced the same issue,

solved my problem.

just make a branch:

git checkout -b "master"

after that

git push -u origin master

bomm.

hope it will be solved.

Alamin
  • 1,355
  • 8
  • 24
20

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

git add .
git commit -m 'initial commit'
git push origin master
xuri
  • 792
  • 6
  • 18
20
  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,030
  • 21
  • 100
  • 124
Alwan Mortada
  • 268
  • 3
  • 8
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,030
  • 21
  • 100
  • 124
Gel
  • 2,546
  • 1
  • 14
  • 25
20

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

git commit -m "first commit"
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Badr Bellaj
  • 9,167
  • 1
  • 36
  • 34
20

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,030
  • 21
  • 100
  • 124
Dinith
  • 619
  • 10
  • 20
20

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

user993563
  • 16,927
  • 10
  • 41
  • 55
19

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,030
  • 21
  • 100
  • 124
neoDev
  • 2,629
  • 2
  • 29
  • 61
19

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,030
  • 21
  • 100
  • 124
snap
  • 1,345
  • 1
  • 13
  • 19
18

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

In my case, starting from October-2020, the repos created since then had the main branch instead of the previous master branch. So all I had to do this:

git push -u origin main 
  • you may skip -u flag if the upstream is set( Like in case you had cloned it already)

Bingo! That worked for me! Hope that helps! Happy coding!

Deekshith Anand
  • 1,355
  • 1
  • 14
  • 20
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
Arslan Ahmad khan
  • 4,714
  • 1
  • 23
  • 31
Aaditya Ura
  • 10,695
  • 7
  • 44
  • 70
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,030
  • 21
  • 100
  • 124
d.raev
  • 8,570
  • 8
  • 54
  • 76
16

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 correct branch name.

command below worked for me

git push origin main
Aayush Neupane
  • 843
  • 1
  • 10
  • 25
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,030
  • 21
  • 100
  • 124
user3051460
  • 1,445
  • 21
  • 55
13

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

Gavin
  • 6,679
  • 3
  • 47
  • 71
  • 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
12
git add .

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
HuntsMan
  • 742
  • 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,030
  • 21
  • 100
  • 124
Govind
  • 2,342
  • 1
  • 24
  • 39
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,030
  • 21
  • 100
  • 124
asdasd
  • 127
  • 1
  • 2
11

In end of 2020, GitHub changed its master branch to main branch

I noticed GitHub created a new branch master and this 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 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
Dharman
  • 26,923
  • 21
  • 73
  • 125
  • I gave a similar answer that helped me on this question: https://stackoverflow.com/a/66737426/5996491 – Kale Mar 21 '21 at 20:55
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,030
  • 21
  • 100
  • 124
rabbitwerks
  • 275
  • 1
  • 3
  • 9
10

git push -u origin master

error: src refspec master does not match any.
error: failed to push some refs to 'http://REPO.git'

This is caused by the repository still being empty. There are no commits in the repository and thus no master branch to push to the server.

It worked for me

Resolution

1) git init
2)git commit -m "first commit"
3)git add app
4)git commit -m "first commit"
5)git push -u origin master
10

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

git push origin main

Github Article

shivampip
  • 1,666
  • 17
  • 16
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,030
  • 21
  • 100
  • 124
Lackeeee
  • 459
  • 1
  • 8
  • 13
9

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

cat > README.md

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

git add .
git commit -m "Initial commit"
git push origin master
Roshin Raphel
  • 2,478
  • 3
  • 18
  • 34
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
  • 1,921
  • 3
  • 30
  • 47
Palak Jain
  • 605
  • 6
  • 14
8

Only commit solved this error:

git commit -m "first commit"
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Omid Ahmadyani
  • 1,238
  • 11
  • 14
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,030
  • 21
  • 100
  • 124
Giulio Roggero
  • 1,662
  • 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>>.

Amalgovinus
  • 3,945
  • 1
  • 32
  • 50
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,030
  • 21
  • 100
  • 124
Rajesh Khadka
  • 1,506
  • 1
  • 18
  • 37
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
  • 2,153
  • 1
  • 17
  • 29
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,030
  • 21
  • 100
  • 124
Anubhav Pandey
  • 1,255
  • 1
  • 12
  • 16
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
  • 165
  • 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

Kai - Kazuya Ito
  • 6,454
  • 5
  • 44
  • 50
Almuntasir Abir
  • 301
  • 4
  • 12
7

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 repo link here"
git push -u origin master

Thanks. Hope it helps.

Vishal
  • 81
  • 1
  • 2
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,094
  • 7
  • 43
  • 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,030
  • 21
  • 100
  • 124
Ferhat KOÇER
  • 3,394
  • 23
  • 25
6

I ran into the same snag..and the solution was to push the code to the repo as though it were an existing project and not a brand new one being initialised.

git remote add origin https://github.com/Name/reponame.git
git branch -M main
git push -u origin main
RileyManda
  • 2,376
  • 22
  • 30
6

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

git push origin main

You can see the result in this photo

Git problem

6

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
  • 123
  • 3
  • 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
6

As one of the options for solving your problem:

git push origin HEAD
gorevanova
  • 469
  • 1
  • 6
  • 15
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,030
  • 21
  • 100
  • 124
Zhengming Ying
  • 437
  • 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,030
  • 21
  • 100
  • 124
Paket2001
  • 370
  • 1
  • 5
  • 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
  • 491
  • 1
  • 9
  • 16
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,030
  • 21
  • 100
  • 124
Amir.S
  • 640
  • 8
  • 13
5

Try git show-ref

You might see refs/heads/live

This means you should do

git push -u origin live
Jeremiah Flaga
  • 4,463
  • 2
  • 23
  • 19
5

I forgot to commit then ran to this. JUST COMMIT

Napster Scofield
  • 1,181
  • 12
  • 11
5

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

gemfield
  • 3,048
  • 5
  • 25
  • 25
5

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
SaltyCatFish
  • 69
  • 1
  • 3
  • 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
5

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)

nz_21
  • 4,552
  • 5
  • 24
  • 57
5

For Repositories WIKI, I encountered this error also.

git show-branch

if shows master then

git push -u origin master
Mark
  • 1,943
  • 7
  • 17
4

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,030
  • 21
  • 100
  • 124
Vojtech Vitek
  • 21,979
  • 4
  • 31
  • 34
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,030
  • 21
  • 100
  • 124
espradley
  • 2,108
  • 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,030
  • 21
  • 100
  • 124
Gujarat Santana
  • 8,851
  • 16
  • 49
  • 73
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,030
  • 21
  • 100
  • 124
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,159
  • 2
  • 13
  • 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
  • 646
  • 7
  • 16
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
  • 6
4

You may run into this issue for multiple reasons.

1. Pending commit for the stagged files

If you've added the changes by running git add command(i.e git add .), and never committed those files then after and tried to push the branch into the remote repository. In this case, you'll face the error src refspec master does not match any.

2. Invalid local branch name

If you did a typo in name of branch,(i.e mster instead of master) then it ill lead you to this error. means the branch you're trying to push into is not in the local repository.

Kiran Maniya
  • 7,224
  • 8
  • 49
  • 71
4

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
  • 125
  • 1
  • 11
  • 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
4

I had faced 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.

Arya Mohanan
  • 177
  • 4
  • 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
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,030
  • 21
  • 100
  • 124
equivalent8
  • 12,962
  • 7
  • 77
  • 103
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,030
  • 21
  • 100
  • 124
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,030
  • 21
  • 100
  • 124
Sohail
  • 546
  • 6
  • 22
3

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

git push -u origin master

subsequent commands worked for me

start with these commands

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

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

git remote
git branch

remote -->origin

branch --> main

git push -u remote branch

or more specifically :

git push -u origin main
cherry
  • 308
  • 1
  • 2
  • 8
3

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

2

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,030
  • 21
  • 100
  • 124
Pramod
  • 786
  • 1
  • 6
  • 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
2

Error from Git:

error: src refspec master does not match any.

Fixed with this step:

git commit -m "first commit"

Before I ran:

git add <files>

And after I ran:

git push -u origin master
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
2

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,030
  • 21
  • 100
  • 124
Janac Meena
  • 2,505
  • 24
  • 29
2

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,030
  • 21
  • 100
  • 124
discipleartem
  • 171
  • 12
2

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,030
  • 21
  • 100
  • 124
samezedi
  • 583
  • 4
  • 15
2

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,030
  • 21
  • 100
  • 124
saber tabatabaee yazdi
  • 3,175
  • 3
  • 39
  • 52
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

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,129,465
  • 480
  • 4,036
  • 4,755
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.

2

For Gitlab users, updated version of gitlab will now have 'main' branch as default.

So you can try the following:

git push origin main

Reference: Gitlab new git default branch name is main

s-lab
  • 49
  • 3
2

Recently came across this problem, what I did was I checked show-ref using, git show-ref and checked whether

refs/heads/master

was present.

It was present, 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.

psygo
  • 85
  • 2
  • 10
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,030
  • 21
  • 100
  • 124
aimango
  • 1,555
  • 2
  • 15
  • 28
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,030
  • 21
  • 100
  • 124
arush436
  • 1,490
  • 17
  • 18
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,431
  • 1
  • 24
  • 38
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
  • 569
  • 8
  • 26
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,030
  • 21
  • 100
  • 124
oskarko
  • 2,434
  • 1
  • 20
  • 22
1

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Exil
  • 424
  • 11
  • 25
1

Permissions issue? Resolution: fork it

I got this error because I had no push permission to the repository so I had to fork it, And then I did execute pull, commit, and push commands once again on the forked repository.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
MAX
  • 1,444
  • 4
  • 15
  • 22
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,598
  • 3
  • 22
  • 29
1

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

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

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

git config --global user.name 'Your name'
git config --global user.email 'name@mail.com'

Continue:

git remote add origin https://github repo url

To push to the repo:

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

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

Otobong
  • 72
  • 4
1

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

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

git config --global push.default current

Now you can git push without anymore errors!

1
  1. git branch

  2. Rename the branch with -mv flag:

    git branch -mv origin master

After this git branch should show master

1

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

$ git branch
master
* <U+0096>your_branch_name

There is a invisible <U+0096> character which probably got there by pasting branch name. Rename your current branch to correct value and then try push again:

$ git branch -m your_branch_name
Ivan Jovović
  • 5,150
  • 3
  • 29
  • 56
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
  • 7,293
  • 1
  • 28
  • 26
1
git init

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

Bharath Kumar
  • 492
  • 6
  • 7
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 enter above details committed files and pushed successfully.

git push

Ram
  • 91
  • 3
1

Initially every file is in untracked state. We need to add untracked files from the working directory to the staging area, only then git will 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. What you did is, 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 than follow this steps.

git init

git add .

git commit -m "First commit"

1

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

When you use git add ., do you 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 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.

uki
  • 19
  • 3
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,030
  • 21
  • 100
  • 124
barfuin
  • 15,975
  • 10
  • 85
  • 128
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
  • 7,448
  • 5
  • 42
  • 50
  • 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

In my case error occurred when I was pushing changes to the branch which I only had locally. Remote branch was not existed with the same name. I resolved error with following command:

git push --set-upstream origin "your-branch-name"

0

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
  • 23
  • 7
0

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
  • 647
  • 1
  • 1
  • 17
0

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 masters and it worked.

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

As of 2022

-1

I had an extremely long local branch name.

git branch -m new_shorter_branch_name

fixed the problem.

Scotty.NET
  • 12,403
  • 4
  • 39
  • 51
-1

I tried this :

git push -u origin master

and it's working by my side.

Abd Abughazaleh
  • 2,962
  • 1
  • 27
  • 42