54

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://cpb09e@bitbucket.org/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://cpb09e@bitbucket.org/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.

CharlesB
  • 75,315
  • 26
  • 174
  • 199
user1676428
  • 611
  • 1
  • 6
  • 12
  • 1
    What's the output of `git branch` ? – Amber Sep 17 '12 at 01:06
  • Where do I find the git branch? – user1676428 Sep 17 '12 at 01:33
  • 2
    It's a command. `git branch` - just like `git push` or `git remote`. Run it and add the output to your question. – Amber Sep 17 '12 at 04:09
  • 3
    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. – 2540625 Jun 13 '14 at 23:47

2 Answers2

156

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.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • 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... – Jason Oct 02 '20 at 14:22
  • @Jason 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) – VonC Oct 02 '20 at 14:38
18

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.

Brice Lin
  • 424
  • 5
  • 11
  • 5
    No need to do this .. only add a commit as @VonC 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 :)) – securecurve Aug 19 '13 at 08:24
  • Ionică Bizău then why not upvote? This was the correct answer for me. In Jenkins be sure to 'checkout to specific local branch'. – jeremyjjbrown Apr 16 '15 at 22:12
  • Thanks bud, adding a commit would just not do it for me, running your command first worked – Arnaud Bouchot Jan 19 '16 at 13:42