git clone gives warning: “remote HEAD refers to nonexistent ref, unable to checkout.”

This warning: “remote HEAD refers to nonexistent ref, unable to checkout.” happened to me today  and since it was an odd enough warning (I’ve never seen it before in all my years of work with git) I thought it’s worth describing the situation here and how I solved it.

I cloned a remote git repository from a public server (it was stlinux’s u-boot source) onto my workstation. I then added a branch to it and created my changes on it, then did a commit. After that I made a place for the repository on our organization’s git server and pushed the entire repository there:

git push –all our-git-server

At that point browsing the repository in gitk it looked fine – every branch had a remote branch. One thing I noticed though, and it appeared strange to me was taht this repository did not have a ‘master‘ branch, but at that point I disregarded that fact as it all appeared to work just fine…

A colleague of mine then decided to clone this repo and got the warning in question:

warning: remote HEAD refers to nonexistent ref, unable to checkout.

That did look strange as there were no errors at all until that point. I went on to try and clone the repo onto my machine and got the same warning: remote HEAD refers to nonexistent ref…

At that point I remembered that the repository did not have a ‘master’ and it occured to me that perhaps that is the problem. The message did sound like it tries to checkout something that does not exist in that repository and ‘master’, being absent from it and being the default branch which is always being checked out matched perfectly that description.

I then added a master branch to the repository and it all worked and the “unable to checkout” warning did not appear again when I tried to clone it.

So adding a master branch definitely solves the problem…

The part which is still not entirely clear for me is – how come cloning the original repository did not generate the remote HEAD refers to nonexistent ref, unable to checkout. warning? If a missing master branch is what causes this bug to manifest it self (because if that is the real problem then it is definitely a bug in git – nothing says that a repo MUST have a master branch…) – if a missing master is what causes this then this problem should have happened also when I cloned the original repository from git://git.stlinux.com/stm/u-boot.git…

If anyone has an explanation to this misterios “nonexistent ref” warning I’d sure love to hear it 🙂

4 thoughts on “git clone gives warning: “remote HEAD refers to nonexistent ref, unable to checkout.”

  1. I faced a similar problem, and also noticed that the remote repo did not have a master branch, whereas it used to. So I tried to recreate the remote master branch by simply checking out to my local master, and pushing it to the remote repo but I get the following:

    Total 13 (delta 11), reused 0 (delta 0)
    error: unable to resolve reference refs/heads/master: No such file or directory
    remote: error: failed to lock refs/heads/master
    To git_user@10.128.40.81:/home/git_user/MyProj.git
    ! [remote rejected] master -> master (failed to lock)
    error: failed to push some refs to ‘git_user@10.128.40.81:/home/git_user/MyProj.git’

    What I don’t get is how is the master branch missing to begin with, and secondly how can I add a remote master branch? Any input you could give me would be appreciated.

  2. I just received this nice message as well. I tried to fix it by adding a Master branch on our git-server but somehow that didn’t work. I can’t clone it to add a master or any branch. So how did you add the Master branch? Can you do this on the bare git repo where you clone from???

  3. Hello All,

    I also faced the similar issue and found out two resolutions to solve it.

    Solution 1:
    ———–
    Make sure the Submit/Push privileges are appropriately assigned to users/groups in Gerrit for the current project. Then make an initial commit on the cloned repo, copy that commit-id and create a new branch in Gerrit – Admin->Projects->Branches->Create New Branch by giving Branch Name as master and Initial Revision as copied commit-id. This creates master branch on repo. And further clones will not get the warning mentioned above.

    Solution 2:
    ———–
    While creating the project,
    ssh -p 29418 gerrit create-project –name –empty-commit # –empty-commit creates the initial commit and master branch gets created.

    • You refer to gerrit in your comment…. I know of gerrit but have never used it. I know its’s some sort of web front-end to git providing code review and tracking features… Does your comment apply to a pure-git system or it’s gerrit-specific?

Leave a Reply

Your email address will not be published. Required fields are marked *