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://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
  • 86,532
  • 28
  • 194
  • 218
user1676428
  • 671
  • 1
  • 7
  • 13
  • 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

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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
  • git push -f origin master – sarjeet singh Dec 23 '21 at 11:22
  • @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. – VonC Dec 23 '21 at 16:21
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.

Brice Lin
  • 532
  • 5
  • 11