Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change naming convention of tags inside a git repository?

Tags:

git

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?

like image 216
Lea Hayes Avatar asked Jun 16 '14 13:06

Lea Hayes


People also ask

Can I change git tag name?

Git does not (and it should not) change tags behind users back. So if somebody already got the old tag, doing a git-pull on your tree shouldn't just make them overwrite the old one. If somebody got a release tag from you, you cannot just change the tag for them by updating your own one.

How do you change the name of a tag?

Tap the tag you wish to rename to bring up its task list. Tap the Title Bar at the top of the screen and then tap Edit tag. Tap the tag name field and edit the name. Tap Save.


2 Answers

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
like image 116
VonC Avatar answered Sep 30 '22 06:09

VonC


Follow the 3 step approach for a one or a few number of tags. For large number of tags the above script can be modified to use the below 3 steps as an alternative solution.

Step 1: Identify the commit/object ID of the commit the current tag is pointing to

     command: git rev-parse <tag name>
     example: git rev-parse v0.1.0-Demo
     example output: db57b63b77a6bae3e725cbb9025d65fa1eabcde

Step 2: Delete the tag from the repository

     command: git tag -d <tag name>
     example: git tag -d v0.1.0-Demo
     example output: Deleted tag 'v0.1.0-Demo' (was abcde)

Step 3: Create a new tag pointing to the same commit id as the old tag was pointing to

     command: git tag -a <tag name>  -m "appropriate message" <commit id>
     example: git tag -a v0.1.0-full  -m "renamed from v0.1.0-Demo" db57b63b77a6bae3e725cbb9025d65fa1eabcde
     example output: Nothing or basically <No error>

Once the local git is ready with the tag name change, these changes can be pushed back to the origin for others to take these.

like image 20
vikas pachisia Avatar answered Sep 30 '22 05:09

vikas pachisia