drake035 drake035 - 7 months ago 136
Git Question

Error: src refspec master does not match any. when trying to push to Bitbucket repo

I created a new repo on

and followed the very simple steps prescribed. First I did
git init
in my local folder to be version controlled. Then I did:

git remote add origin https://my_account@bitbucket.org/my_account/my_project.git

That worked. The last step was:

git push -u origin master

I obtained the following:

error: src refspec master does not match any.
error: failed to push some refs to 'https://my_account@bitbucket.org/my_account/my_project.git'

Why do I get this error?

Answer Source

You have no master branch.

Consider the steps you did:

  1. git init: this created a new, empty repository, with no commits.

    Given that there are no commits, which branch or branches is / are the commits on? (This is a somewhat philosophical question, but it deserves your best attempt at an answer.)

  2. git remote add origin url: this added a remote, which is basically just a short name for a URL. Your otherwise empty repository now has one remote, named origin, which is short for the URL.

  3. git push origin master: this asks your Git to push, to the other Git at the URL specified by the short name origin, all the commits that are on your master branch, that are not on their master branch, and then set their master branch to point to the same specific branch tip commit as your master branch.

Now, count all the commits in your repository. How many are there? How many commits should you expect your Git to push in step 3? How many commits are on branch master? Can branch master even exist if there are no commits on it? (This gets us back to the philosophical question in step 1.)

Now suppose you git add some files to your index (the index is where you build the next commit), and then run git commit. How many commits will you have? Which branch or branches will the commits be on? (This question is surprisingly tricky in the end, although in fact the obvious answer—"all of the new commits, which is therefore one single commit, will be on branch master"—is the correct answer.)

In Git's world, a branch can never be empty—it can never have no commits. A branch that has no commits simply does not exist. Weirdly, however, you can be on such a branch. (These branches have two different adjectives used to describe or create them, depending on which Git command or output you're looking at: unborn branch and orphan branch.)

What it means to be on a branch in Git is that your next commit, the one you build up in the index, will extend that branch, adding a new commit to it. So the solution to your problem is to make at least one commit while you are on branch master, so that branch master comes into existence with that first commit you make. Then you will be able to push it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download