18

I am cloning a single branch from a repository and creating a tag in a python script. The commands are as follows.

git clone -b master --single-branch <repository adress>

git tag -a testag -m 'test'

It clones successfully but when it comes to adding the tag, it breaks with the following error:

fatal: Failed to resolve 'HEAD' as a valid ref.
5
  • 1
    What does git status say after your clone?
    – FlyingFoX
    Commented Oct 1, 2013 at 13:08
  • # On branch master nothing to commit, working directory clean
    – Ahmad
    Commented Oct 1, 2013 at 13:33
  • What does cat .git/HEAD say after the clone?
    – FlyingFoX
    Commented Oct 1, 2013 at 15:00
  • ref: refs/heads/master
    – Ahmad
    Commented Oct 1, 2013 at 16:09
  • cat .git/refs/heads/master should return a sha1 of a commit. Can you check if that commit exists?
    – FlyingFoX
    Commented Oct 1, 2013 at 17:26

5 Answers 5

27

I had the same issue. You have to commit first before tagging

git commit

because you put tags on commits. So when there is no commit (like in your situation), you can't create a tag.

26

I ran into the same issue and was able to fix it by changing from

git tag -a testtag -m 'test'

to

git tag -a testtag -m "test"

I was running in Windows 7. Hope this helps :-)

1
  • 1
    Using Cmder on W10, this is what actually helped. Commented Aug 3, 2016 at 11:59
8

I also faced git tag: fatal: Failed to resolve 'HEAD' as a valid ref issue when I was missing -m in the following command.(during tag creation)

git tag -a testtag 'test'

changing to

git tag -a testtag -m 'test'

fixed the issue

5

I had the same problem. I cloned from a bare repo and tried to use 'git tag' in that cloned repo, and that is where I was getting the error. To fix it I had to at least one push up to master before I could begin tagging. Hope this helps.

3

If you have access to the remote repository

cd /path/to/remote/repository
git config --bool core.bare true
2
  • 2
    What does that do precisely? Commented Jan 23, 2014 at 17:56
  • According to this page bitflop.com/document/111 "A "bare" repository in Git just contains the version control information and no working files (no tree) and it doesn't contain the special .git sub-directory. Instead, it contains all the contents of the .git sub-directory directly in the main directory itself."
    – raffi
    Commented Jul 31, 2014 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.