Git error: src refspec master does not match any error: failed to push some refs [duplicate]
Asked Answered
C

2

60

I am trying to add a file to my repository on BitBucket and I am having trouble.

I am using GIT and this is what I type in

$ cd lis4368/assignments
$ git remote
$ git remote -v
$ git remote rm origin

and then I type this in (this is what BitBucket tells me to enter)

$ git remote add origin https://[email protected]/cpb09e/cpb09e.git
$ git push -u origin master

And I keep getting this error message:

error: src refspec master does not match any.
error: failed to push some refs to 'https://[email protected]/cpb09e/cpb09e.git'

Can someone pleas help me out? I have tried everything from git commit to rm -rf * and I cannot get anything to work at all.

Cacophonous answered 17/9, 2012 at 1:1 Comment(4)
What's the output of git branch ?Wispy
Where do I find the git branch?Cacophonous
It's a command. git branch - just like git push or git remote. Run it and add the output to your question.Wispy
I just encountered this problem, and it seemed to be caused by my not adding a custom commit message above the default commit message (I figured, why write "initial commit", when it clearly says that very same thing in the Git-generated text below it). The problem resolved when I removed the .git directory, re-initialized the project directory for Git, re-added the GitHub remote, added all files to the new stage, committed with a personal message above the auto-generated message, and pushed to origin/master.Selfexistent
C
161

One classic root cause for this message is:

  • when the repo has been initialized (git init lis4368/assignments),
  • but no commit has ever been made

Ie, if you don't have added and committed at least once, there won't be a local master branch to push to.

Try first to create a commit:

  • either by adding (git add .) then git commit -m "first commit"
    (assuming you have the right files in place to add to the index)
  • or by create a first empty commit: git commit --allow-empty -m "Initial empty commit"

And then try git push -u origin master again.

See "Why do I need to explicitly push a new branch?" for more.

Caecilian answered 9/10, 2012 at 6:7 Comment(4)
To caveat, prior to > git commit -m "Your Message" Make sure you add all files you want committed with > git add . Because you can't commit files without adding them to the list. To recap, you first add the files to be committed. You then commit the files with a message. And lastly push the files...Pee
@Pee I agree, and I have edited this 8 years-old answer to make the add step more explicit. I have also added an alternative solution (create a first initial empty commit)Caecilian
git push -f origin masterSunk
@sarjeetsingh If you already have a remote master branch that you need to replace by your own new local master branch, then yes, you would need --force. Make sure to warn your colleagues though, for them to reset their own local cloned repository to the new branch history.Caecilian
E
21

It doesn't recognize that you have a master branch, but I found a way to get around it. I found out that there's nothing special about a master branch, you can just create another branch and call it master branch and that's what I did.

To create a master branch:

git checkout -b master

And you can work off of that.

Endocrinotherapy answered 5/6, 2013 at 5:23 Comment(4)
No need to do this .. only add a commit as @Caecilian said, and master will be created and pushing will work fine... either way you will have to commit on something ... so you offer an additional unneeded step .. thanks anyway :))Incrassate
Ionică Bizău then why not upvote? This was the correct answer for me. In Jenkins be sure to 'checkout to specific local branch'.Tumescent
Thanks bud, adding a commit would just not do it for me, running your command first workedAbirritate
git push -f origin masterSunk

© 2022 - 2024 — McMap. All rights reserved.