如何在Git中重命名注释标签 [英] How to rename an annotated tag in Git

查看:135
本文介绍了如何在Git中重命名注释标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Git中,现有的带注释的标签如何重命名?我有几乎一百个表示版本号的标签,并且每个标签都注释了有用的说明该版本已更改的内容。我想改变我为这些标签使用的命名风格,并记录标签消息,删除标签,并用旧消息重新创建它,并且新名称将成为近乎一百个标签手动执行的噩梦。一个脚本或一系列git命令可以实现自动化。

解决方案

坏消息是,一个带注释的标签对象被嵌入在该带注释的标签对象内。因此,如果你想彻底彻底,你需要替换这些标记对象:


  • 如果它们是 signed 标签,您将需要使用新签名重新生成它们。

  • 其他可以将标签对象复制到新的标签对象,系统名称更改您想要。例如, filter-branch 命令执行此操作。 (因为你必须使用一些低级别的管道命令,这有点难看,因为没有简单方便的方法,但请参阅下面的内容。)




好消息是,没有绝对要求您使用正确的注释标签和轻量级标签。也就是说,一个带注释的标签实际上是一对实体:一个轻量级标签(带有一些名称,只是 refs / tags / 名称空间中的一个条目),以及注释标记对象(在存储库本身中),通常在其标记字段中具有相同的名称。但您可以制作一个新的轻量级标签,指向现有的带注释标签对象,然后可以选择删除原始轻量级标签:

  $ git cat-file -p v2.5.0 | sed's / @ /'
object a17c56c056d5fea0843b429132904c429a900229
type commit
tag v2.5.0
tagger Junio C Hamano< gitster pobox.com> 1438025401 -0700

Git 2.5
----- BEGIN PGP签名-----
版本:GnuPG v1

iQIcBAABAgAGBQJVtoa5AAoJELC16IaWr + bLRtQP / 0RYjVe9fLubiN5vLaAJ98B5
K3apw8bScJ4bZQJiOGMZg7AJ8pSB9XchqopjNlO2v8XVrZEkFPQ7ln3ELjOITusO
0MIZz6Y4sS0yqMklNPWx / OIzvSyKOs8quUA / J87Ha + pnMYlobDgWJxDnZ3hVO6q2
0lVMRUvwM9qsBiPsVKyAba5qPDBn9uTjgR / hivA3Ha97foq / qMM6rjERp5hX8KTE
JacLMlx7ZSAJiNKmz1mVk + xyDVGDh9nojiz93jRYohAM8gsbyyRayPGKlKsMrajC
s0bLxilV8zupNrMOs54ND71VqNo992ewiCrB3FBpTah2rPE0MKsxCY72pFiZp / HN
w1I3seQbd880d2TXfczVVphG3xN3xbfnC + aEqobgPuFIgGxHqeWqUpDQnWa0XhtK
i5phhENEjbMdCDGf7ylRRvCTL5O2Mz1XnNfZBP0uqIX6PyzaCwcZM1DUP0xY8Nvz
xo0BplMvK4sAr + fCW0HHHlDO6Ky3jjRyPUEyVEbwF50lZ3Sgzkkf32I06yeZgQW9
GE6 + qGopw7JVQM95UDMBysbQSNuTmwnclwn0DBfc85vQwZw5OwnRk0RsQxcnqNr2
B4Nyubl8Pge4H0T0pahpAdr0AU7JHv4i4yYZh1Dr4d + hzzXAK + ospCxkcJVX7Qz5
BP8pbLPj3ubLAenxg71I
= xfTv
----- END PGP SIGNATURE-- ---
$ git rev-parse v2.5.0
8d1720157c660d9e0f96d2c5178 db3bc8c950436
$ git tag傻的v2.5.0
$ git rev-parse傻的
8d1720157c660d9e0f96d2c5178db3bc8c950436
$ git tag -d v2.5.0
已删除的标签'v2.5.0' (was 8d17201)
$ git for-each-ref refs / tags / silly
8d1720157c660d9e0f96d2c5178db3bc8c950436 tag refs / tags / silly

(之后我恢复了旧标记并删除了愚蠢版本,所以没有经过全面测试,但至少没有爆炸。)



要自动复制标签或重新创建标签(带或不带签名),只需使用 git for-each-ref 找到所有旧的标签,并使用结果名称制作新标签。使用 git cat-file -p 来漂亮地打印现有标签以进行修改以制作新标签,或者只需将新标签指向现有标签对象即可。在打印标签(到管道或文件)后,您可以使用sed或其他可编程编辑器编辑标签,并将结果提供给 git tag -F 作为输入文件或 git mktag 到创建一个标签对象,并将生成的SHA-1提供给 git tag 来制作相应的轻量级标签。



(注意:如果你已经通过编写一个bash脚本来完成所有这些工作,并且只需要帮助完成它或者纠正一些问题,就可以发布脚本。)

b $ b

How can an existing annotated tag be renamed in Git?

I have almost a hundred tags representing version numbers on a repository, and each tag is annotated with a useful description about what was changed that version. I'd like to change the naming style I've used for these tags, and recording the tag message, deleting the tag, and recreating it with the old message and a new name would be a nightmare to do manually for almost a hundred tags. A script or a series of git commands to do this would be useful for automation.

解决方案

The bad news is that the name of an annotated tag object is embedded inside that annotated tag object. So if you want to be totally thorough, you'll need to replace those tag objects:

  • If they're signed tags you will need to regenerate them with new signatures.

  • Otherwise, it's possible to copy the tag objects to new tag objects, with the systematic name changes you'd like to make. The filter-branch command does this, for instance. (It's a bit ugly since you have to use some low level plumbing commands; there's no easy convenient method. But see below.)

The good news is that there's no absolute requirement that you use the "correct" annotated tag along with a lightweight tag. That is, an annotated tag is actually a pair of entities: a lightweight tag (with some name that's simply an entry in the refs/tags/ name-space), along with an annotated tag object (in the repository itself) that normally has the same name in its tag field. But you can make a new lightweight tag that points to the existing annotated tag object, then optionally delete the original lightweight tag:

$ git cat-file -p v2.5.0 | sed 's/@/ /'
object a17c56c056d5fea0843b429132904c429a900229
type commit
tag v2.5.0
tagger Junio C Hamano <gitster pobox.com> 1438025401 -0700

Git 2.5
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAABAgAGBQJVtoa5AAoJELC16IaWr+bLRtQP/0RYjVe9fLubiN5vLaAJ98B5
K3apw8bScJ4bZQJiOGMZg7AJ8pSB9XchqopjNlO2v8XVrZEkFPQ7ln3ELjOITusO
0MIZz6Y4sS0yqMklNPWx/OIzvSyKOs8quUA/J87Ha+pnMYlobDgWJxDnZ3hVO6q2
0lVMRUvwM9qsBiPsVKyAba5qPDBn9uTjgR/hivA3Ha97foq/qMM6rjERp5hX8KTE
JacLMlx7ZSAJiNKmz1mVk+xyDVGDh9nojiz93jRYohAM8gsbyyRayPGKlKsMrajC
s0bLxilV8zupNrMOs54ND71VqNo992ewiCrB3FBpTah2rPE0MKsxCY72pFiZp/hn
w1I3seQbd880d2TXfczVVphG3xN3xbfnC+aEqobgPuFIgGxHqeWqUpDQnWa0XhtK
i5phhENEjbMdCDGf7ylRRvCTL5O2Mz1XnNfZBP0uqIX6PyzaCwcZM1DUP0xY8Nvz
xo0BplMvK4sAr+fCW0HHHlDO6Ky3jjRyPUEyVEbwF50lZ3Sgzkkf32I06yeZgQW9
Ge6+qGopw7JVQM95UDMBysbQSNuTmwnclwn0DBfc85vQwZw5OwnRk0RsQxcnqNr2
B4Nyubl8Pge4H0T0pahpAdr0AU7JHv4i4yYZh1Dr4d+hzzXAK+ospCxkcJVX7Qz5
BP8pbLPj3ubLAenxg71I
=xfTv
-----END PGP SIGNATURE-----
$ git rev-parse v2.5.0
8d1720157c660d9e0f96d2c5178db3bc8c950436
$ git tag silly v2.5.0
$ git rev-parse silly
8d1720157c660d9e0f96d2c5178db3bc8c950436
$ git tag -d v2.5.0
Deleted tag 'v2.5.0' (was 8d17201)
$ git for-each-ref refs/tags/silly
8d1720157c660d9e0f96d2c5178db3bc8c950436 tag    refs/tags/silly

(I restored the old tag and deleted the "silly" version after this, so it's not terribly thoroughly tested, but it didn't blow up at least.)

To automate either copying the tags, or re-creating the tags (with or without signatures), simply use git for-each-ref to find all the old tags, and use the resulting names to make the new tags. Use git cat-file -p to pretty-print the existing tag for modifying to make the new tag, or simply make the new tag pointing to the existing tag object. After printing the tag (to a pipe or file) you can edit it with sed or some other programmable editor, and feed the result to git tag -F as an input file, or to git mktag to create a tag object, feeding the resulting SHA-1 to git tag to make the corresponding lightweight tag.

(Note: if you've gotten partway through writing a bash script to do all this, and just need help to finish it up or correct some issue(s) with it, post the script-so-far.)

这篇关于如何在Git中重命名注释标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆