Skip to content

Instantly share code, notes, and snippets.

@knu
Created May 13, 2009 14:38
Show Gist options
  • Save knu/111055 to your computer and use it in GitHub Desktop.
Save knu/111055 to your computer and use it in GitHub Desktop.
How to mass-rename tags and push them with Git
# Rename tags named foo-bar-#.#.# to v#.#.# and push the tag changes
git tag -l | while read t; do n="v${t##*-}"; git tag $n $t; git push --tags ; git tag -d $t; git push origin :refs/tags/$t ; done
@SofiaSousa
Copy link

Renamed all my x.y.z tags to vx.y.z, thanks!

@bendem
Copy link

bendem commented Feb 18, 2019

you can greatly speed this up by pushing and pruning all remote tags at once:

# make sure your tags are up to date
git fetch origin
# rename all tags
git tag -l | while read t; do n="v${t##*-}"; git tag $n $t; git tag -d $t; done
# synchronise with the server
git push --tags --prune origin refs/tags/*

@tomblenz
Copy link

you can greatly speed this up by pushing and pruning all remote tags at once:

# make sure your tags are up to date
git fetch origin
# rename all tags
git tag -l | while read t; do n="v${t##*-}"; git tag $n $t; git tag -d $t; done
# synchronise with the server
git push --tags --prune origin refs/tags/*

no matches found: :refs/tags/*

@zkrige
Copy link

zkrige commented Aug 28, 2020

This changes x.y.z to x/y/z, but won't delete tags that are already in new format

# make sure your tags are up to date
git fetch origin
# rename all tags
git tag -l | while read t; do n=$(echo $t | sed "s?\.?/?g"); if [[ "$n" != "$t" ]]; then git tag $n $t; git tag -d $t; git push origin :refs/tags/$t ; done

@llinardos
Copy link

This changes build/n to build/0n (e.g.: build/8 -> build/08).

git tag -l | while read t; do n=$(echo $t | sed "s?\/?/0?g"); git tag $n $t; git tag -d $t; done

@PavelSavushkinMix
Copy link

Rename all vx.y.z tags to x.y.z:
git tag -l | while read t; do n="${t#?}"; git tag $n $t; git push --tags ; git tag -d $t; git push origin :refs/tags/$t ; done

Don't forget to turn off CI/CD, if it's based on the tags ;)

@fleveillee
Copy link

If you have a mixture of vx.y.z and x.y.z tags, the first command will add a v to your existing 'v' tags, creating vvx.y.z tags...

This shell script solves that issue:

#!/bin/bash

# Get all of the repo's tags
all_tags=$(git tag)

# Loop through all tags
for tag in $all_tags; do
    # Only process tags not starting with "v"
    if [[ $tag != v* ]]; then
        # Create the vx.y.z tag
        git tag v$tag $tag
        # Delete the x.y.z tag
        git tag -d $tag
        # Pushes the new vx.y.z tag and deletes the x.y.z tag remotely
        git push origin  v$tag :$tag
    fi
done

# Push all tags, shouldn't be necessary, just there as backup
git push --tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment