-
Star
(336)
You must be signed in to star a gist -
Fork
(76)
You must be signed in to fork a gist
-
-
Save okunishinishi/9424779 to your computer and use it in GitHub Desktop.
if any of the remote tags do not exist, the delete all approach bails out!
This will get the tags that are on the remote 'origin', then delete those tags:
git ls-remote --tags origin | cut -f 2 | xargs git push --delete origin
to delete refs as well:
git ls-remote origin | cut -f 2 | grep -iv head | xargs git push --delete originI didn't find a solution anywhere that didn't requre a single git push call per tag, so I came up with this variant, which - in my case - reduced the runtime from several hours to several seconds:
git push --delete origin $( git ls-remote --tags origin | awk '{print $2}' | grep -Ev "\^" | tr '\n' ' ')Explanation
git push --delete origin $(...): Deletes a tag (or multiple) on origin$( git ls-remote --tags origin | awk '{print $2}' | grep -Ev "\^" | tr '\n' ' '): Creates a space delimited string of all tagsgit ls-remote --tags origin: Prints all tags on the remote origin... | awk '{print $2}' | ...: Only prints the second column of the previous command output... | grep -Ev "\^" | ...: Filters out unwantedrefs/tags/mytag^{}variants (not sure where they come from)... | tr '\n' ' ': Converts the list into a space delimited string
It takes advantage of the fact that you can provide multiple tag names in a space delimited string, so it only invokes git delete once.
YMMV (use carefully):
deletes all (locally known) tags on remote named 'origin'
git push origin $(git tag -l --format=':%(refname)')you may want to delete the tags locally also, or you might push them again:
git tag -d $(git tag -l)Worked wonders thanks!
git tag -d $(git tag -l) ;
git fetch --tags ; \
git tag -l | grep "test" | (xargs -n 1 git push --delete origin && xargs git tag -d) ; \
- Delete all local tags
- Fetch all remove tags
- loop through and delete the tag that matches your tag pattern
This variant, inspired by @markusressel, deletes all tags from a remote Git repository in a single push using xargs, making it faster and cleaner than deleting them individually.
Script:
REMOTE_NAME=origin
git ls-remote --tags "$REMOTE_NAME" \
| awk '{print $2}' \
| grep -v '\^{}$' \
| xargs -r git push --delete "$REMOTE_NAME"How it works:
REMOTE_NAME=origin→ sets the remote name.git ls-remote --tags "$REMOTE_NAME"→ lists all remote tags, with each line showing<SHA> <refname>.awk '{print $2}'→ extracts the refname (e.g.,refs/tags/v1.0.0).grep -v '\^{}$'→ removes dereferenced annotated tags ending with^{}.xargs -r git push --delete "$REMOTE_NAME"→ deletes remaining tags in a single push.
nice, The "git tag -l | xargs git tag -d" in the example above might work better with "grep "