这个问题是关于获取被克隆的标签名称以进入分离的 HEAD 状态,而不是如何从分离的 HEAD 状态中恢复。
我们有一个构建系统,可以通过用作版本的标签克隆各种 git repos。每个 repo 都会构建一个由标签名称进行版本控制的输出文件。这很有效,但是当同一个提交中有两个标签时会出现问题。正在使用的 git 命令不处理这种情况。
例如,我有一个这样的 repo,在同一个提交上有两个版本标签。
git init
touch .gitignore
git add .
git commit -am "Initial commit"
git tag release-v1
git tag release-v2
如果我想用版本“release-v2”构建这个仓库,我们的构建系统会像这样检查这个仓库:
git clone --single-branch --depth 1 -b release-v2 repo.git
从 repo 中的 Makefile 中,我们使用此命令从标签名称中获取版本:
VERSION := $(shell git describe --abbrev=0 --tags)
这个命令似乎总是指向第一个“release-v1”标签,而不一定是从中克隆的标签。
我发现这个命令确实返回了两个标签,但它仍然没有告诉我克隆了哪个标签。
$ git tag --contains $(git rev-parse HEAD)
release-v1
release-v2
是否有一个替代命令总是会返回从 repo 克隆的标签,或者 git 甚至不知道这一点,因为标签只指向提交 ID?
这个构建系统是非常定制的,肯定有一些问题,所以如果不需要太多大的更改,我愿意完全接受替代的 checkout 方法。
最佳答案
是的,这是可能的。
只需更改构建系统运行的命令:
git clone --single-branch --depth 1 -b release-v2 repo.git
到
git clone --single-branch --depth 1 -b release-v2 --no-tags repo.git
--no-tags
选项从 git 版本 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.
还使用 git 2.14.0 进行了验证,它可以按您的预期工作:
🍓π 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
关于git clone - 分离的 HEAD - 获取从中克隆的标签名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60400330/