Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git clone - detached HEAD - Get tag name that was cloned from

Tags:

git

This question is about getting the tag name that was cloned from to get into a detached HEAD state, not how to recover from a detached HEAD state.

We have a build system that clones various git repos by tags which are used as versions. Each repo builds an output file that is versioned by the tag name. This works well enough, but a problem arrises when there are two tags on the same commit. The git commands that are being used do not handle this situation.

For example, I have a repo like this with two version tags on the same commit.

git init
touch .gitignore
git add .
git commit -am "Initial commit"
git tag release-v1
git tag release-v2

If I wanted to build this repo with version "release-v2", our build system would check out this repo like this:

git clone --single-branch --depth 1 -b release-v2 repo.git

From a Makefile inside the repo we are using this command to get the version from the tag name:

VERSION := $(shell git describe --abbrev=0 --tags)

This command seems to always point to the first "release-v1" tag though and not necessarily the tag that was cloned from.

I found this command which does return both tags, but it still doesn't tell me which tag was cloned.

$ git tag --contains $(git rev-parse HEAD)
release-v1
release-v2

Is there an alternative command that would always return the tag that the repo was cloned from or does git not even know that because the tag just points to the commit ID?

This build system is very custom and definitely has some issues, so I am open to alternative checkout methods altogether if it doesn't require too many large changes.

like image 993
EarlCrapstone Avatar asked Jan 26 '26 00:01

EarlCrapstone


1 Answers

Yes, it's possible.

Simply change the command your build system runs from:

git clone --single-branch --depth 1 -b release-v2 repo.git

to

git clone --single-branch --depth 1 -b release-v2 --no-tags repo.git

The --no-tags option has been available since git version 2.14.0:

--no-tags
    Don’t clone any tags, and set remote.<remote>.tagOpt=--no-tags in the config, ensuring that future git pull and git fetch
    operations won’t follow any tags. Subsequent explicit tag fetches will still work, (see git-fetch(1)).

    Can be used in conjunction with --single-branch to clone and maintain a branch with no references other than a single
    cloned branch. This is useful e.g. to maintain minimal clones of the default branch of some repository for search
    indexing.

Also verified with git 2.14.0, and it works as you'd expect:

πŸ“Ο€ shang@raspberrypi:/tmp/git $ git version
git version 2.14.0
πŸ“Ο€ shang@raspberrypi:/tmp/git $ 
πŸ“Ο€ shang@raspberrypi:/tmp/git $ git clone --single-branch --depth 1 -b release-v2 --no-tags https://github.com/sding3/stackoverflow60400330.git             
Cloning into 'stackoverflow60400330'...
<...snipped...>

πŸ“Ο€ shang@raspberrypi:/tmp/git/stackoverflow60400330 $  git describe --abbrev=0 --tags
release-v2
πŸ“Ο€ shang@raspberrypi:/tmp/git/stackoverflow60400330 $ git tag --contains $(git rev-parse HEAD)
release-v2
like image 165
Shang Jian Ding Avatar answered Jan 27 '26 14:01

Shang Jian Ding



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!