I have a git repository which contains a number of tags with the format "v1.2.3-rc-4" which I would like to automatically rename to "1.2.3-rc4". The tags exist in both the local and remote repositories.
I should clarify that the numeric components in the version number should be treated as variables. The above values are simply to demonstrate the format of the tags.
Is there a way to automate this change?
解决方案
To list all tags, I would recommend git for-each-ref with the --shell option to eval refs.
Combine it with a one-liner to rename/delete a tag.
#!/bin/sh
git for-each-ref --shell --format="ref=%(refname:short)" refs/tags | \
while read entry
do
# assign tag name to $ref variable
eval "$entry"
# test if $ref starts with v
ref2="${ref#v}"
if [[ "${ref}" != "${ref2}" ]]; then
# rename/delete tag
git push origin refs/tags/${ref}:refs/tags/${ref2} :refs/tags/${ref}
git tag -d ${ref}
fi
done