gitpushbarerefspec

git push new file to a bare repo failed: "error: src refspec master does not match any." Why?


I was trying this:

mkdir ~/gitremote
cd ~/gitremote
git init --bare

I can see file names like

HEAD        config      hooks       objects
branches    description info        refs

OK, then in another directory,

git clone trosky@localhost:/Users/trosky/gitremote
vi readme (add one line)
git add .
git commit -m "1st file"
git push origin master

Then it gives an error:

$git push origin master
error: src refspec master does not match any.
error: failed to push some refs to 'trosy@localhost:/Users/trosky/gitremote'

I searched google and it says this kind of error is due to empty folder on remote repo. But the remote repo is not empty, and locally I'm committing a file that's not empty either. Why this error still prompts out?

How to fix it? Thanks.


Solution

  • Try with the syntax:

    git push -u origin master
    

    Since you are pushing to an empty repo, you need to explicitly push master.
    Otherwise, the default push.policy being simple, Git would look for a branch named master in your remote repo (and since your remote bare repo is empty, it has no master branch yet)

    Of course, you won't see any readme in your remote bare repo, since it is a bare repo: no working tree.

    what's the difference between 'git clone /Users/trosky/gitremoe' and 'git clone 'trosy@localhost:/Users/trosky/gitremote'

    One is using the file protocol also called local protocol, the other ssh protocol. You don't need ssh here.