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

Viewed 127750

This seems like a popular error for different causes.

I've got a simple bare git repo called "kiflea.git", I clone it like this:

git clone git://kipdola.be/kiflea.git

Then git tells me: warning: remote HEAD refers to nonexistent ref, unable to checkout.

And yes, there are no versioned files in the map, except for the .git directory. Anyway, the only thing I need to do is:

cd kiflea
git checkout master

And it works, all the files are there. But I thought cloning a repo automatically checks out the master, so what is going on exactly, and how do I fix it?

I have noticed that, after I do the git checkout master bit, this gets added to my local .git config file:

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

It's probably interesting to know that this git repository used to be an svn repository in a distant past.

Ps: when browsing the bare repository using gitweb, there clearly is a master branch there: http://kipdola.be/gitweb/?p=kiflea.git;a=summary

13 Answers

It is late(2021) answer but will help others.

Quick fix run this git command git symbolic-ref HEAD refs/heads/main

Thanks @alexey-vazhnov

When you create a bare repo using git init --bare it set ref: refs/heads/master in HEAD file but when you clone this bare repo its default branch is main, this is the issue so you need to change HEAD file and put main' instead of masater` i.e.

ref: refs/heads/main

For Gitlab, even it shows you are on a default branch (e.g. master) you might not actually be on it, setting it again fix it, like so:

  1. Create a new branch, maybe asd
  2. settings > repository > Default Branch, which shows the default branch is master
  3. Set it to asd
  4. Set it back to master
  5. Delete asd branch

Done, now your default branch is master

I had same issue when creating a bare repo.

I solved it just cloning the repo, creating a local master branch and then pushing master to the remote repo.

1) clone the repo

$ git.exe clone --progress -v "the remote path" "my local path"

2) create a master branch locally.

   $ git checkout -b master

3) commit something in the local branch

$ git add readme.md 
$ git commit –m “Added readme”

4) Push local master on the remote

   $ git push origin master

This question is 10 year old. This is how I solved it now:

Suppose you are cloning your repo using clone command than you will see logs like this:

git clone https://abcgit-repo.abccoolrepo

Cloning into 'abcgit404.sasasasmy cool repo'... remote: Counting objects: 198, done remote: Finding sources: 100% (26/26) Receiving objects: 80% (176/219)ed 208 (delta 0)Receiving objects Receiving objects: 100% (219/219), 49.09 KiB | 163.00 KiB/s, done. warning: remote HEAD refers to nonexistent ref, unable to checkout.

Now to solve remote HEAD refers to nonexistent ref, unable to

First checkout your branch you will see logs like this:

  1. git checkout remotes/origin/mainline

HEAD is now at xyz

  1. See all branchs your current branch will have * in front of it

git branch -a

*(HEAD detached at origin/mainline)
remotes/origin/a
remotes/origin/b
remotes/origin/c
remotes/origin/d
remotes/origin/mainline

  1. Copy the branch name from above and run git checkout checkout -b

Eg checking out mainline:

git checkout -b remotes/origin/mainline

*origin/mainline remotes/origin/a
remotes/origin/b
remotes/origin/c
remotes/origin/d
remotes/origin/mainline

This will solve the issue

In my case the repo was empty.

git checkout --orphan master

git add some_file
git commit -m 'init'
git push origin master 

If there is no master branch actually available, check the following; If there is a file named 'packed-refs' inside the '.git' folder, open it and you could find all references listed.

Something like below;

# pack-refs with: peeled fully-peeled 
e7cc58650190bd28599d81917f1706445d3c6d8b refs/tags/afw-test-harness-1.5
^cfae4f034e82591afdf4e5ed72279297d0eee618
6afe1bcfa4bd74de8e0c8f64d024e1cc289206df refs/tags/afw-test-harness-2.1
^c32f7fa495d4b44652f46c065fcd19c3acd237a6
72f2e4284dfbf27c82967da096c6664646bbdd19 refs/tags/android-1.6_r1
^50992e805e758e2231f28ec2127b57a1a9fd0ddc
0cbd528cad1cee9556098b62add993fc3b5dcc33 refs/tags/android-1.6_r1.1

Then use;

git checkout refs/tags/xxxx

Or

git checkout 'HASH value'

to checkout the required version. Thank you.

I seemed to fix it with:

git checkout -b  master
git push

This created the default master, and then I could checkout my other branches

Related