重命名git标记会导致不一致

问题描述 投票:18回答:4

用git标签“1.0”重命名为“1.5”后

git tag 1.5 1.0
git tag -d 1.0
git push origin :refs/tags/1.0

我的git存储库似乎处于不一致状态。这是git describe输出:

warning: tag '1.0' is really '1.5' here
1.0-97-g88085b2

它现在应该返回1.5-...

git fsck --tags输出:

Checking object directories: 100% (256/256), done.
tagged commit aad9477bba4bcf44ea34ea9693aeffc98527ff01 (1.0) in b96ce67583239e198f9e2aff5175176d65779044
Checking objects: 100% (3975/3975), done.

如何删除已删除标记的悬空引用?这是重命名标签的正确方法吗?

git git-tag
4个回答
27
投票

几分钟前我遇到了同样的问题。已经提出的答案都没有涉及真正的问题,即摆脱warning: tag 'foo' is really 'bar' here消息并让git describe只列出标签的新名称。这在我的情况下尤其重要,因为我的构建系统使用git describe来记录构建中使用的源代码。

Replicating the Problem

我可以这样做来复制这个问题:

$ git tag foo --annotate -m"original message"
$ git tag bar foo
$ git tag -d foo
$ git describe 
warning: tag 'foo' is really 'bar' here
foo

(上面的--annotate标志是多余的,因为-m暗示--annotate,但我把它包括在内以强调。)我试图用轻量级标签复制问题,但是无法这样做。因此,为了复制问题,需要注释。

Fixing the Problem

其中一些涉及推动已经被推动的东西,但我发现自己与大卫·库尔普同意,当he says

然而,有时候,不准确(杂乱)历史的长期痛苦是不值得的,短期的痛苦是值得的。

一旦你坚持使用warning: tag 'foo' is really 'bar' here,那么你必须做到:

$ git tag bar bar -m"original message" --force
$ git describe 
bar

如果消息需要更改,请根据需要进行调整。

如果旧标签已被推送,则删除旧标签:

$ git push origin :refs/tags/foo

要更新新标记(如果已经推送):

$ git push origin refs/tags/bar

Avoiding the Problem

要首先避免这个问题,你必须创建bar

$ git tag bar foo -m"original message"

3
投票

当有人建议重写历史记录时(或者在这种情况下,重复历史记录)重申标准谨慎 - 如果你可以避免它,那就不要这样做。

然而,有时候,不准确(杂乱)历史的长期痛苦是不值得的,短期的痛苦是值得的。

如果是这种情况,下面的文章给出了所需的步骤:How to Rename a Tag Already Pushed to a Remote git Repo

基本步骤是:

git tag new_tag old_tag
git push --tags
git push origin :refs/tags/old_tag
git tag -d old_tag

0
投票

不知道这会有多大用处,但是在使用release-it时遇到了这个警告。在我的远程仓库中,同一标签名称有轻量级和带注释的标签条目:

$ git ls-remote --tags origin
302883ef0cb2df8975abfbd24bbe89f64cf3da31    refs/tags/0.0.1
4852192308b404d74d7a4088c19a4629299f6ea2    refs/tags/0.0.1^{}

(注意第二个条目上标签名称后面的^{}

对于带注释的标签(?),这似乎是正常的,所以不要认为这本身就是问题,但是在删除所有标签并重新标记所有内容后问题就消失了。例如。对于上述标签......

git tag -d 0.0.1                # Delete local tag
git push --delete origin 0.0.1  # Delete remote tag
git tag -a -m "" 0.0.1 4852192308b404d74d7a4088c19a4629299f6ea2  # Recreate [annotated] tag
git push --tags                 # Push tag(s) to remote repo

-3
投票

不,我不认为这是git中标签的正确工作流程。

git的一个基本规则是:不要搞乱你已经推过的东西。

由于您已经推送了标签1.0,因此您不希望在本地将其重命名为1.5,然后尝试推送它。保留后代的1.0标签,创建一个新的1.5标签,并推动它。真的 - 这就是标签的用途。因此,您可以在6个月后返回并重新创建1.0版本的软件。

© www.soinside.com 2019 - 2024. All rights reserved.