How to Rename Annotated Tag in Git

How can I modify an existing annotated tag in Git?

I have almost a hundred tags representing version numbers in the repository, and each tag is annotated with a useful description of what has been changed by this version. I would like to change the naming style that I used for these tags, and record the tag message, delete the tag and recreate it with the old message, and the new name will be a nightmare to manually do for almost a hundred tags, A script or a series of git commands for this would be useful for automation.

+4
source share
2 answers

The bad news is that the name of the annotated tag object is embedded inside the annotated tag object. Therefore, if you want to be completely thorough, you will need to replace those tagged objects:

  • If they are tagged, you will need to restore them with new signatures.

  • Otherwise, it is possible to copy tag objects into new tag objects, with changes to the systematic name that you want to make. For example, the filter-branch command does this. (This is a bit ugly, since you need to use some low-level plumbing commands, there is no simple convenient method, but see below.)

The good news is that there is no absolute requirement that you use the โ€œcorrectโ€ annotated tag along with the light tag. That is, the annotated tag is actually a pair of objects: a light tag (with some name, which is simply an entry in the refs/tags/ namespace), along with an annotated tag object (in the repository itself), which usually has the same name in the tag field. But you can create a new light tag that points to an existing annotated tag object, and then optionally delete the original light 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 โ€œstupidโ€ version after that, so it is not very thoroughly tested, but at least it did not explode.)

To automate either tag copying or tag re-creation (with or without signatures), simply use git for-each-ref to find all the old tags and use the resulting names to create new tags. Use git cat-file -p to print an existing tag for modification to create a new tag or just make a new tag that points to an existing tag object. After printing the tag (into a pipe or file) you can edit it with sed or some another programmable editor and pass the result of git tag -F as input or git mktag to create a tag object by submitting the received SHA-1 to git tag to make the corresponding light tag.

(Note: if you got a portion of a bash script entry to do all this, and you just need help to finish it or fix some problems with it, post the script - this far.)

+5
source

To summarize torek answer for the case when you want to rename a single annotated tag v1.0.0 only 1.0.0

Option 1: Rename Replace both light tag and base tag object

Warnings:

  • This will break the signatures of the signed tags. Use only tags with unsigned annotation.
  • The naive replacement for sed , as shown below, will also change the occurrence of v1.0.0 in the tag message and in the tagger name tag (if any). Use a more specific RegEx if that is not what you intend.
 git tag "1.0.0" $(git cat-file -p "v1.0.0" | sed 's/v1\.0\.0/1.0.0/g' | git mktag) # You might want to check the result before continuing to delete the old tag. # Delete the old tag locally git tag -d v1.0.0 # Delete the old tag on the default remote (origin) git push origin :v1.0.0 

Option 2: Rename Replace only the light tag and point it to the original tag object

 git tag "1.0.0" "v1.0.0" # Delete the old tag locally git tag -d v1.0.0 # Delete the old tag on the default remote (origin) git push origin :v1.0.0 
+2
source

Source: https://habr.com/ru/post/1498152/


All Articles