git “fatal: no matching remote head”

git clone is the command you use to clone and start working with a remote repository. Only problem is, this command does not work if the remote repository is empty. Instead it responds with this cryptic error message:

$ git clone ssh://
Initialized empty Git repository in autobuildrequires/.git/
fatal: no matching remote head

I sent a rant about this to the git developers last month. The response was basically that I’m stupid and should have read the manual page to git-push. Let’s do that now:

git-push – Update remote refs along with associated objects

Updates remote refs using local refs, while sending objects necessary to complete the given refs.

You can make interesting things happen to a repository every time you push into it, by setting up hooks there. See documentation for git-receive-pack(1).

Do I think it’ll be worth my time following the link to git-receive-pack?

Here’s how to solve the problem

I don’t pretend to understand git, but I think that a git repository doesn’t exist unless it has at least one commit. Firstly we need to create a repository on the local machine and add a commit to it:

mkdir autobuildrequires
cd autobuildrequires
git init
touch README
git add README
git commit -a -m "First commit."

Now we have a repository containing one commit. You can check this with the git log command.

Next we have to tell git that the origin of the local repository is the remote repository. The origin is where git checks by default when you do a git pull. So do this, obviously replacing the URL with the correct URL for your repository:

git remote add origin ssh://

Now the final, confusing step. We have to connect the upstream origin with the current local branch (usually called master). This involves editing a config file:

vi .git/config

and adding the following at the bottom:

[branch "master"]
  remote = origin
  merge = refs/heads/master

(You should see the [remote "origin"] section in the config file already, which is what the previous git remote add origin command did).

We’re not quite over yet. For reasons completely unknown, you now have to do:

git push origin master

and that should make your commit appear remotely. At this point, plain git pull and git push should work as expected.

What a palaver!


Filed under Uncategorized

13 responses to “git “fatal: no matching remote head”

  1. Matt W

    It’s not unknown – the final push is because git commits are all to your local branch. Nothing gets transferred between branches without pushing or pulling between them. The Subversion/CVS model of a central repository that you check out from and commit to simply doesn’t exist – not even to the extent that Bazaar can support it.

  2. rwmj

    Thanks Matt. I should clarify what I meant there was that plain git push doesn’t work. You have to first do git push origin master, and after that git push alone works.

  3. FWIW, I normally don’t bother initializing an empty remote repository; instead, I just use git clone with the –bare option.

    Upload the generated repository, tweak some options (for HTTP download, etc.) and presto. You’d still need to edit .git/config, but there’s no need for the git remote gobbledygook.

  4. rwmj

    Thanks Michel. The problem in my case was that someone else had created the repository for me.

    Anyway it’s fixed now, and I’ve got handy instructions for next time I do this.

  5. Having the .git on the end of the remote origin call:

    git remote add origin ssh://

    did not work for me. I kept getting:

    …not a git repository…

    I had to remove the .git and add a ‘/’

    hope this helps someone else.

    Thanks for the other information.

  6. Thanks, this worked great for me.

  7. Thanks.
    however, i was able to get it working without editing config file

  8. Pingback: Setting up a git repository on « Smart procrastination

  9. Pingback: Giordano's Nest » Private git repositories on Site5

  10. Pingback: Sven’s blog » Blog Archive » share your git repository

  11. thanks dude, I was really pissed with whats happening.

    Subversion was so easy to use.

  12. Alain

    THANK YOU ! Finally an understandable post about git… I have been trying hard to create my first git project… This is done ! Thanks !

  13. After cloning an empty repository, and before having pushed:
    $ git remote show origin
    * remote origin
    Fetch URL: edward@someserver:/git/ticker-data.git
    Push URL: edward@someserver:/git/ticker-data.git
    HEAD branch: (unknown)
    Local branch configured for ‘git pull’:
    master merges with remote master

    Notice the HEAD branch: (unknown)

    This can be resolved by adding a file and pushing:

    $ git add README
    $ git commit -m ‘initial commit (readme)’
    $ git push origin master

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.