Message 'src refspec master does not match any' when pushing commits in Git
Asked Answered
R

141

4107

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'
Ritchey answered 15/11, 2010 at 6:9 Comment(21)
@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.Allowedly
Did you set up your git config commands to install and configure git globally on your machine?Vazquez
Hope this post would be useful to somebody- samranga.blogspot.com/2015/07/… The error in the question can be popped even when tried to Create a git BitBucket repository from an already locally existing projectWhistle
I received this error trying to push the wrong branch name. Resolved using git status to get the proper one.Groff
Adding a comment to call out @aug2uag's alternative answer - sleepily skipping git commit can cause this error, as well!Besprinkle
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.Reviere
If you didnt add git add with dot or some files this error also will appear.Recruitment
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.Ostrogoth
Here 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:<branch>Ferrin
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 firstEntertaining
Or you may wrote non-existence branch.Acidophil
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.Tahmosh
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.Errecart
will you honestly just be corteous enough to accept an answer?Dunkle
@sinoohe, if you have only main branch, try "git push origin HEAD :main " It worked for me.Jennajenne
Is an initial commit missing?Quan
Now 142 answers (incl. deleted ones)Larkin
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 workMasbate
For me I had to do git pull origin main, after that I could push my changes. I believe this overwrites your branch thoughDeterminate
Yet another reason to use MercurialNoted
Thanks for expressing this question in such explanatory way.Cruciform
M
5816

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!

Musicianship answered 27/9, 2011 at 16:7 Comment(32)
Don't just follow this step blindly, look at what @Vi has mentioned, and then modify your push command to correct ref.Fleawort
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.Glowworm
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.Kashmir
Fixes different issue which is nice but not really an answer to This actual question.Synonym
Hope this post would be useful to somebody- samranga.blogspot.com/2015/07/… The error in the question can be popped even when tried to Create a git BitBucket repository from an already locally existing projectWhistle
Also happens if you type the name of your branch incorrectly. Typos.Marzipan
git commit -m 'initial commit' should be double quoted. 'git commit -m "initial commit", at least on windows.Linger
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 pushAllerie
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!Kelleykelli
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 scheduler it works. HAHA :)Urbannal
Another possible reason: you don't actually have a branch called masterSpume
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.Tetrahedron
@Musicianship this does not work if I git remote set-url origin <my_git_repo.git> and try to git add . , git commit -m "intial commit', git push origin masterMyongmyopia
Oops! Never committed! section worked for me , i had even committed but there was some issue that's why it make error for meExtended
This is what happens when you follow BitBucket's (incomplete) instructions for setting up your repo. Lol.Proletariat
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...Chapbook
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.Keewatin
Or you may wrote non-existence branch.Acidophil
I forgot to commit then ran to this. JUST COMMITGeelong
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".Wallboard
github rename the master with main. so you can use git remote origin mainEntertaining
I ran into this and the problem was I hadn't set my branch git branch -M <branch_name>Homelike
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!Nisbet
giving initial commit solved my problem: git commit -a -m"commit message"Coercion
master instead of main solve me problemKaiak
I tried your method but still getting error: src refspec master does not match anyGenvieve
I also recommand watching this tutorial : youtu.be/yZdmcMQkQRoUrban
Github now changed git push origin master to git push origin mainKilligrew
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)Hochheimer
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.Stubbed
Instead of master when I put main, it worked.Cruciform
thanks @CarlosAS, that was the problem in my caseCadelle
H
1776
  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).
Humfrid answered 15/11, 2010 at 11:24 Comment(22)
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:Ritchey
git checkout -b testbranch ; git push origin testbranch:masterRitchey
git show-ref showed my branch; the git push origin HEAD:<branch> worked for me.Doubleheader
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?Convincing
@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 <branch> instead of master in case you attempt to git checkout -b <branch> before any FIRST push after initial git remote add origin <url>Accrete
@Humfrid 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..Storeroom
@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.Humfrid
What if no refs/heads/master found ??Altis
@SunilSharma, Then use some ref that is found, or explicit hex commit-id.Humfrid
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.Electroencephalogram
@Trip, Note that there is also option --force-with-lease, which is safer that usual --force, especially when you use the repository not alone.Humfrid
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 <remote> HEAD:dev worked perfectly.Garlaand
This happened to me when trying to psh a tag that didn't exist.Tollbooth
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.Ragged
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.Mob
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.Skeie
master is changed to main now.Backgammon
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 masterGuildhall
putting the HEAD: before the remote branch name worked like a magic.Practise
I did not get a Gerrit url to indicate the patch was updated but it was!Confuse
git push origin HEAD:master --force. ... add (--force) if not workingInopportune
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. ThanksGummy
O
285

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.
Overstrung answered 4/1, 2012 at 17:3 Comment(4)
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!Prestissimo
See this earlier answer. I suspect that you needed to add a file because git won't track empty directories.Filberte
push --force could also completely blew away co-workers hard work. Added warning by it.Synonym
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 AryoMejias
M
153
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.

Mccrory answered 9/8, 2017 at 9:22 Comment(2)
I checked it's working. Please ignore -u option and then tryMccrory
The issue here seems to be completely different than the OP's...but it seems many including me had this issue.Espy
S
140

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
Sanfo answered 2/9, 2014 at 1:56 Comment(0)
C
130

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.

Celiaceliac answered 25/9, 2011 at 1:44 Comment(7)
This probably works because git doesn't actually track directories, only files. So if a directory is empty, git won't actually add it.Filberte
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.Synonym
Such a simple solution to a frustrating problem. I was testing the creation and clonining of repos and created empty directories not files.Candiot
8 years later this saved me some headache!Cunaxa
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.Tremendous
@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?Celiaceliac
Yes it's a typo. Thank youTremendous
S
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://[email protected]': 
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
Severe answered 3/11, 2012 at 20:30 Comment(4)
that's correct, the business end is more specifically git remote add __REMOTE_NAME__ __URL_OR_SSH__, and above the remote name is "origin"Severe
They added and committed in their question so that was not the issue even though this helps other people.Synonym
This answer did work however because of the -u option that was used here.Synonym
What’s git *create remote?Mikesell
F
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
Fransis answered 12/1, 2015 at 17:30 Comment(0)
T
71

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
Tracay answered 14/6, 2021 at 7:28 Comment(3)
Run git add . then git commit -m "first commit", then push againBehring
git branch -M main worked for me.Laspisa
This git branch -M main is important which is why it wasn't working for me. It expects master if you don't put anything.Sap
M
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'
Mealworm answered 23/5, 2012 at 17:43 Comment(7)
LOL. I was trying to push to origin master but that branch didn't exist. It was called origin stable.Dolph
The -u may have helped here.Synonym
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;Imperforate
I just got this when I misspelled the branch name.Shugart
My local branch was spelled "sheduler" and I was doing git push origin scheduler. HA! One letter off will kill you in programming. lolUrbannal
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.Tailwind
this saves my day!!! I mixed it up with main and masterReason
S
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.

Seville answered 25/10, 2018 at 1:33 Comment(1)
this is the only one that actually worked.Copenhaver
P
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
Pulsifer answered 28/4, 2018 at 7:12 Comment(0)
S
62

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
Sabra answered 16/12, 2020 at 18:47 Comment(1)
Everything above didn't help, but finally this did. It can be many different issues with the same error message.Barbaric
M
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
Moonstruck answered 15/11, 2010 at 6:9 Comment(2)
How is this the error in question related to .gitignore?! If it is related (which I highly doubt) you should explain it in your answer. ThanksConception
Thanks, I was missing the ignore document.Adah
P
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.

Predecessor answered 13/1, 2020 at 6:1 Comment(1)
did you mean ` git add --all` with two dashesMarsland
S
33

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

Scum answered 25/4, 2019 at 14:37 Comment(0)
D
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.

Drescher answered 9/10, 2020 at 16:17 Comment(1)
The original announcement from GH CEO Nat Friedman on Twitter in response to the #BLM Movement. twitter.com/natfriedman/status/1271253144442253312Automotive
I
28

Just add an initial commit. Follow these steps:

  • git add .

  • git commit -m "initial commit"

  • git push origin master

This worked for me.

Inexperienced answered 28/12, 2017 at 7:2 Comment(0)
C
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
Constitutionality answered 28/2, 2021 at 12:20 Comment(1)
What is up with the bumping, changing the date without changing anything else?Larkin
A
28

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.

Aesir answered 2/6, 2021 at 11:1 Comment(2)
I got the same issue and realized that branch was on main. then I followed this and got resolved.Disorganization
same here. Thanks!Mediocrity
B
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.

Blowgun answered 12/12, 2014 at 19:38 Comment(0)
S
26

Maybe the branch is main instead of master.

Try

git push origin HEAD:main

or

git push origin main

Strained answered 20/5, 2021 at 18:21 Comment(1)
This was my problem. Tried so many things and then realized it was main instead of master.Domini
H
25

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)

Hypophosphate answered 30/10, 2020 at 6:20 Comment(0)
A
21

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

Antabuse answered 3/12, 2011 at 13:29 Comment(0)
T
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.

Trenttrento answered 11/6, 2015 at 14:15 Comment(0)
M
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
Muirhead answered 2/3, 2018 at 14:36 Comment(0)
J
21

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

git commit -m "first commit"
Jenninejennings answered 8/9, 2019 at 9:56 Comment(0)
P
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
Pignus answered 12/9, 2019 at 7:48 Comment(0)
L
20

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

git add .
git commit -m 'initial commit'
git push origin master
Lateral answered 4/5, 2014 at 14:18 Comment(1)
please do write git commit -m "initial commit"Twosome
M
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.)

Monge answered 11/2, 2017 at 21:49 Comment(0)
O
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.

Obvious answered 22/3, 2019 at 16:16 Comment(0)
B
18

I was facing the same issue and tried most of the answers here, But the issue was because of recent changes of GitHub renaming.

GitHub is gradually renaming the default branch of repositories from master to main.

https://github.com/github/renaming

Your new command would be:

git push origin main

instead of this:

git push origin master
Backgammon answered 16/10, 2020 at 11:7 Comment(2)
Thanks, this works for me. other solutions didn't.Motch
Same happened to me just now. I just used master and it worked, instead of main.Porush
T
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!

Trigeminal answered 2/10, 2020 at 16:27 Comment(3)
Even that failed. My local branch was master. I created a main locally and then push worked with -u flagByssinosis
Thanks a lot. It worked. This was really a major recent update by githubLicketysplit
Thanks, this was also a recent update for gitlab, just in case anyone was wondering.Flexuosity
M
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
Melanymelaphyre answered 17/4, 2021 at 15:5 Comment(1)
Another answer claims at the end of 2020.Larkin
A
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.

Ablaut answered 24/5, 2017 at 16:32 Comment(0)
G
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
Geometrician answered 5/12, 2015 at 11:58 Comment(0)
L
13

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

Learned answered 8/10, 2013 at 15:48 Comment(1)
I suspect a great many of us who came here via web search actually mistyped the name!Einstein
B
13
git add .

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

Broderic answered 30/9, 2016 at 5:36 Comment(0)
S
12

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

git config --global user.email "[email protected]"

git config --global user.name "Your Name"
Savage answered 30/10, 2012 at 5:48 Comment(0)
G
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.

Gem answered 10/12, 2016 at 10:21 Comment(0)
R
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
Rech answered 10/3, 2021 at 19:23 Comment(1)
I gave a similar answer that helped me on this question: https://mcmap.net/q/12206/-failed-to-push-some-refs-to-email-160-protectedGleanings
R
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.

Rajiv answered 22/1, 2019 at 4:33 Comment(0)
U
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
Undersexed answered 11/8, 2020 at 10:22 Comment(0)
P
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
Psittacine answered 7/6, 2022 at 9:15 Comment(0)
D
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
Dinka answered 1/9, 2015 at 9:44 Comment(0)
S
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
Stitching answered 2/7, 2020 at 13:7 Comment(0)
R
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

Rech answered 19/5, 2021 at 14:4 Comment(0)
S
8

Try this:

git add .

git commit -m "your commit message"

git remote add origin *remote repository URL*

git push origin *your-branch-name*
Standby answered 12/2, 2018 at 13:33 Comment(0)
T
8

Only commit solved this error:

git commit -m "first commit"
Tamikotamil answered 6/8, 2018 at 20:1 Comment(0)
C
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
Cilia answered 5/4, 2022 at 18:28 Comment(0)
F
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
Falda answered 25/5, 2022 at 16:3 Comment(0)
B
7

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

git checkout master
git commit -a -m "your comment"
git push origin master
Babul answered 17/1, 2014 at 9:24 Comment(0)
C
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>>.

Ceremony answered 21/8, 2015 at 1:14 Comment(0)
T
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"
Tamasha answered 24/8, 2017 at 17:39 Comment(0)
M
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
Momentary answered 14/2, 2018 at 11:57 Comment(0)
D
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.

Dateline answered 14/4, 2018 at 9:21 Comment(0)
H
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
Hautbois answered 24/11, 2019 at 22:15 Comment(0)
O
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
Oestrin answered 25/6, 2020 at 17:29 Comment(1)
Had the same problem and it turn out that I typo branch name.Obmutescence
K
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.

Kingston answered 24/9, 2021 at 10:0 Comment(2)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewUropod
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 problemKingston
E
6

I had a similar error. But Git tells me:

*** Please tell me who you are.

Run

git config --global user.email "[email protected]"
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.

Emerson answered 10/10, 2013 at 10:38 Comment(1)
I migrated to a new machine and was getting these errors. Setting my GIT info fixed it.Actualize
M
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
Moran answered 17/7, 2015 at 14:29 Comment(0)
K
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"
Keelykeen answered 2/4, 2018 at 13:5 Comment(1)
No, I mean if you forget git commit -m "xxxx" command, you get the same problem.Thank you I edited my commentNathalia
T
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
Thunderbolt answered 9/10, 2020 at 14:17 Comment(0)
O
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).

Ousley answered 25/11, 2020 at 16:28 Comment(3)
It was 92 answers (incl. deleted).Larkin
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.Larkin
Another answer indicates the origin of the change is on GitHub.Larkin
M
6

As one of the options for solving your problem:

git push origin HEAD
Miamiami answered 23/3, 2021 at 9:26 Comment(0)
C
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.

Chirography answered 26/11, 2012 at 2:11 Comment(0)
W
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
Washko answered 3/1, 2016 at 22:20 Comment(0)
L
5

Maybe GitHub doesn't know who you are.

First you have to run:

git config --global user.email "[email protected]"
git config --global user.name "Your Name"
Lodging answered 24/11, 2018 at 5:13 Comment(1)
Perfect answer !!Averroes
T
5

Try git show-ref

You might see refs/heads/live

This means you should do

git push -u origin live
Touslesmois answered 20/8, 2020 at 23:59 Comment(3)
I think you mean "main" not "live". As of June 2020 they've dropped "master" in favor of "main.Wallboard
Hi @JosephBrenner, I already removed the reference to Github.Touslesmois
Tried this solution and got refs/heads/master. Then git push origin master worked.Amphictyony
G
5

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

Geelong answered 20/9, 2020 at 8:6 Comment(2)
Glad I wasn't the only oneEquestrienne
Yeah, it happens ;)Geelong
F
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.

Fingerling answered 25/10, 2020 at 20:57 Comment(2)
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 doTrunnel
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.Fingerling
R
5

For Repositories WIKI, I encountered this error also.

git show-branch

if shows master then

git push -u origin master
Regularize answered 18/12, 2021 at 5:14 Comment(2)
What about if it doesn't shown anythingMetallophone
What is "Repositories WIKI"? Can you elaborate and/or provide a reference?Larkin
L
5

I had a similar issue. In my .gitignore file there was an asterisk, '*', defined. All the files were being ignored and therefore it failed.

I removed '*' and used all the standard Git commands that worked for me.

git add .
git commit -m "Initial commit"
git push -u origin main
Leathern answered 19/9, 2023 at 14:21 Comment(0)
O
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.

Outskirts answered 13/3, 2014 at 18:48 Comment(1)
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.Filberte
G
4

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

Gaskins answered 16/4, 2015 at 15:18 Comment(0)
B
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.

Behlau answered 10/1, 2016 at 17:45 Comment(0)
X
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".

Xanthate answered 17/5, 2017 at 18:26 Comment(0)
F
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.

Forester answered 11/4, 2020 at 21:56 Comment(0)
P
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 ...

Palecek answered 14/6, 2020 at 15:32 Comment(0)
G
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.

Greathouse answered 1/7, 2020 at 21:12 Comment(0)
F
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

Fishhook answered 14/10, 2020 at 9:13 Comment(1)
Please review Why not upload images of code/errors when asking a question? (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 (it covers answers as well). Thanks in advance.Larkin
C
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.

Coopersmith answered 20/10, 2020 at 8:40 Comment(0)
E
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
Expansile answered 23/10, 2020 at 18:44 Comment(2)
That is valid only for new repositories. So this answer is completely inaccurate. More info here: github.com/github/renamingGoldston
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.Expansile
B
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

Bassoon answered 2/2, 2022 at 18:50 Comment(0)
I
4

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 and only then will Git consider it as commit and then pushing resolved the error.

Inexpressible answered 22/12, 2022 at 9:47 Comment(0)
B
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
Banditry answered 27/7, 2012 at 2:6 Comment(1)
Why did you need to use the full URL for your remote instead of just setting up an alias for it with git remote add origin <url>?Filberte
P
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
Pfister answered 7/11, 2012 at 23:21 Comment(0)
R
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
Reniti answered 29/8, 2013 at 14:1 Comment(0)
D
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

Dulciedulcify answered 4/7, 2017 at 18:22 Comment(0)
R
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

Riccardo answered 5/12, 2017 at 16:17 Comment(0)
B
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
Bumgardner answered 27/2, 2018 at 12:15 Comment(0)
E
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
Electoral answered 29/3, 2018 at 4:6 Comment(0)
C
3

I had the same problem and discovered also that I had not committed any file, so when I tried to commit again, I got this error message:

*** Please tell me who you are.

Run

 git config --global user.email "[email protected]"
 git config --global user.name "Your Name"

 to set your account's default identity.
 Omit --global to set the identity only in this repository.

 fatal: unable to auto-detect email address (got 'USER@WINDOWS-HE6I2CL.(none)')

Then all I did was add my email and name globally and then committed again:

git commit -m 'Initial commit'

Then pushed

git push -u origin master
Costello answered 12/7, 2018 at 11:47 Comment(0)
E
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
Entertaining answered 14/12, 2018 at 2:17 Comment(0)
F
3

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
Following answered 5/10, 2020 at 4:3 Comment(0)
G
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.

Gentoo answered 5/10, 2020 at 19:55 Comment(1)
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.Gentoo
L
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!

Lick answered 19/2, 2021 at 11:28 Comment(0)
G
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.

Greensward answered 14/3, 2021 at 8:0 Comment(0)
W
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"

Watkins answered 30/10, 2021 at 6:51 Comment(0)
I
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.

Interject answered 15/3, 2022 at 2:52 Comment(1)
push is always done on that branch which is selected.Valet
P
3

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

Putt answered 25/6, 2022 at 4:6 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewDrona
L
3

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

git branch -M main
Lixiviate answered 3/7, 2023 at 23:51 Comment(0)
H
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!!

Hamlen answered 12/10, 2019 at 4:31 Comment(0)
F
2
  1. git branch

  2. Rename the branch with the -mv flag:

    git branch -mv origin master

After this, git branch should show master.

Fusillade answered 6/3, 2021 at 6:42 Comment(2)
The purpose of git branch ought to be explained. Please respond by editing (changing) your answer, not here in comments (but *** *** *** *** *** without *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today).Larkin
Ivan Jovović's answer has a clue.Larkin
B
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
Bridgettebridgewater answered 29/7, 2021 at 15:12 Comment(0)
K
2

There are mainly two 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 it, and it will be good. :)

Kissel answered 8/2, 2023 at 15:57 Comment(0)
A
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)

Adin answered 23/3, 2023 at 17:1 Comment(0)
P
2

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

To solve this issue:

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

A few months later...

I just found another cool way of doing this:

git config --global init.defaultBranch main

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

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

Polarize answered 10/8, 2023 at 6:29 Comment(0)
V
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.)

Voroshilovsk answered 19/9, 2012 at 18:40 Comment(0)
B
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.

Brendabrendan answered 29/9, 2014 at 12:59 Comment(0)
B
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
Brethren answered 23/4, 2015 at 15:27 Comment(0)
L
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.

Latashialatch answered 25/7, 2016 at 10:1 Comment(0)
S
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.

Slocum answered 18/8, 2018 at 14:10 Comment(1)
Re "skipped": Do you mean "escaped"?Larkin
K
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
Kayser answered 24/9, 2018 at 18:33 Comment(0)
V
1

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

Vinegary answered 30/12, 2018 at 21:38 Comment(0)
S
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.

Shipmate answered 25/1, 2019 at 14:25 Comment(1)
What do you mean by "fork"? Fork as on GitHub? Or something else? What did you do exactly?Larkin
R
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>)

Ravi answered 1/6, 2020 at 13:33 Comment(0)
H
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 '[email protected]'

Continue:

git remote add origin https://github repo url

To push to the repository:

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

It loads the commits to your repository.

Done!!!

Harass answered 3/11, 2020 at 16:41 Comment(0)
S
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
Shuler answered 10/3, 2021 at 10:50 Comment(1)
U+0096 is listed as <control>. What is the most likely equivalent? ASCII 0x16 (SYNC / SYN? AKA Ctrl + V (presumably in a terminal context). It would not be uncommon to use Ctrl + V in a terminal window).Larkin
C
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.

Centrifugal answered 9/4, 2021 at 10:53 Comment(0)
C
1
git init

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

Crissman answered 17/5, 2021 at 8:42 Comment(1)
What do you mean by "initialize //"? A root of some sort?Larkin
M
1
git init
git add .
git commit -m "message"
prompt me to enter email and user name
  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

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

git push
Modification answered 11/6, 2021 at 1:53 Comment(0)
M
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"
Mouldon answered 3/2, 2022 at 9:3 Comment(0)
A
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.

Autoerotism answered 19/2, 2022 at 11:57 Comment(1)
When using Node.js, presumably.Larkin
S
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!

Sorcery answered 8/3, 2022 at 5:0 Comment(0)
M
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

Maharajah answered 17/3, 2022 at 15:11 Comment(0)
N
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.

Nebuchadnezzar answered 23/5, 2022 at 7:30 Comment(2)
Re "masters": Do you mean "master" (without "s")?Larkin
"master" without the "s"Nebuchadnezzar
H
1
 sudo git push -v --progress "origin" : origin/prod_18aug2022

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

Hallucination answered 21/10, 2022 at 11:4 Comment(0)
R
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
Recount answered 22/11, 2022 at 15:42 Comment(0)
Q
1

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

# Type the following on the CMD
git init

# Then try pushing your code online
git push
Quintic answered 19/2, 2023 at 1:31 Comment(0)
I
1

You may encounter this problem when any of your folders is already linked to another Git repository.

Visit the folder that is not being added and delete the .git file (if you do not want it to link to that repository any more) from there and try adding and committing again.

Insurgence answered 21/2, 2023 at 1:11 Comment(0)
U
1

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

git add .    
Umbilication answered 29/3, 2023 at 11:53 Comment(5)
That is more or less identical to baisong's answer, aug2uag's answer, xuri's answer, pratik kumar's answer, Gujarat Santana's answer, and Alwan Mortada's answer. That is just from the first 30 answers.Larkin
It is identical to HuntsMan's answer and neoDev answer.Larkin
And Sumer's answerLarkin
Related: Why do I need 50 reputation to comment? What can I do instead?.Larkin
@PeterMortensen Yes I know they answered before me but I shared my own experience that fixed my issue.Umbilication
H
1

Change master to main. That should work.

git push origin main
Heteronomy answered 9/5, 2023 at 7:8 Comment(3)
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?Effieeffigy
E.g., https://mcmap.net/q/11900/-message-39-src-refspec-master-does-not-match-any-39-when-pushing-commits-in-git. But also, this question isn’t tagged with GitHub and, regardless, was published long before GutHub changed the default branch to main.Effieeffigy
This worked. I too was using git push origin master but should have been using git push origin mainManslaughter
C
1

Sometimes these type of error exits when your GitHub personal-access-token as expired. As of in my case the same error occurred, but go on executing all the solutions none of them worked, instead it showed that my personal-access-token as expired. To know more about personal-access-token do refer these link- https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens

Cavendish answered 30/1 at 10:24 Comment(0)
U
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.

Ulu answered 27/8, 2014 at 21:11 Comment(0)
N
0

I had an extremely long local branch name.

git branch -m new_shorter_branch_name

fixed the problem.

Neisse answered 9/3, 2015 at 15:43 Comment(1)
What is the limit?Larkin
M
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
Macintosh answered 24/10, 2019 at 15:31 Comment(2)
What is "fastlane match nuke development", etc.? Some kind of weird Git client?Larkin
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).Macintosh
M
0

The thing that worked for me was

git commit -m "initial commit"

after running the command.

Instead of running the "git push -u origin master", use:

git push -u origin main
Maupassant answered 15/11, 2023 at 19:31 Comment(1)
This question is 13 years old. It has 157 other answers, including one with a score of over 5,700, another with a score over 1,700, and several at or approaching 100 points. Are you entirely sure that this new answer introduces new information? If so, please explain.Ragged
C
0

If you had the problem where it worked with "master" or with "main", but you want to change the name of the branch:

You can go to the repository settings and change it here:

Changing Master to Main

Chemarin answered 30/11, 2023 at 17:32 Comment(1)
Re "repository settings": Where? What context? What website or client application?Larkin
A
-1

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

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

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 hold people back from serious coding work, but I guess ultimately words do matter ;)

Alitaalitha answered 3/8, 2023 at 15:5 Comment(1)
This solution has already been given (https://mcmap.net/q/11900/-message-39-src-refspec-master-does-not-match-any-39-when-pushing-commits-in-git). 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 biteinteractive.com/of-git-and-github-master-and-main which tells you more about this problem than you ever ever ever wanted to know.Conlin
V
-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"
Vanderpool answered 3/8, 2022 at 0:12 Comment(1)
this does not solve the error these commands are only for showing the current git user and emailDarbies

© 2022 - 2024 — McMap. All rights reserved.