Delete all tags from a Git repository

GitGit Tag

Git Problem Overview


I want to delete all the tags from a Git repository. How can I do that?

Using git tag -d tagname delete the tag tagname locally, and using git push --tags I update the tags on the git provider.

I tried:

git tag -d *

But I see that * means the files from the current directory.

$ git tag -d *
error: tag 'file1' not found.
error: tag 'file2' not found.
...

Consider I have a lot of tags, and I want to delete them, all.

Git Solutions


Solution 1 - Git

git tag | xargs git tag -d

Simply follow the Unix philosophy where you pipe everything.

On Windows use git bash with the same command.

Solution 2 - Git

To delete remote tags (before deleting local tags) simply do:

git tag -l | xargs -n 1 git push --delete origin

and then delete the local copies:

git tag | xargs git tag -d

Solution 3 - Git

It may be more efficient to push delete all the tags in one command. Especially if you have several hundred.

In a suitable non-windows shell, delete all remote tags:

git tag | xargs -L 1 | xargs git push origin --delete

Then delete all local tags:

git tag | xargs -L 1 | xargs git tag --delete

This should be OK as long as you don't have a ' in your tag names. For that, the following commands should be OK.

git tag | xargs -I{} echo '"{}"' | tr \\n \\0 | xargs --null git push origin --delete
git tag | xargs -I{} echo '"{}"' | tr \\n \\0 | xargs --null git tag --delete

Other ways of taking a list of lines, wrapping them in quotes, making them a single line and then passing that line to a command probably exist. Considering this is the ultimate cat skinning environment and all.

Solution 4 - Git

For Windows users using PowerShell:

git tag | foreach-object -process { git tag -d $_ }

This deletes all tags returned by git tag by executing git tag -d for each line returned.

Solution 5 - Git

If you don't have the tags in your local repo, you can delete remote tags without have to take it to your local repo.

git ls-remote --tags --refs origin | cut -f2 | xargs git push origin --delete

Don't forget to replace "origin" to your remote handler name.

Solution 6 - Git

Adding to Stefan's answer which was missing how to delete tags from remote. For windows powershell you can run this to delete the remote tags first followed by the local tags.

git tag | foreach-object -process { git push origin --delete $_ }
git tag | foreach-object -process { git tag -d $_ }  

Solution 7 - Git

For windows users:

This deletes all Local Tags by running git tag and feeding that list to git tag -d:

FOR /f "tokens=*" %a in ('git tag') DO git tag -d %a

(Found on: https://gist.github.com/RandomArray/fdaa427878952d9768b0)

Solution 8 - Git

You can also use:

git tag -d $(git tag)

Solution 9 - Git

I have to delete the tags with prefix

for example, I have to delete the tags v0.0.1, v0.0.2, v0.0.3, v0.0.4, v0.0.5

git tag -d $(git tag -l "v0.0.*")

Decompose and explain the statement above:

  1. To list all the tags with prefix

    git tag -l "v0.0.*"

  2. To delete tags

    git tag -d $tag_names

That's how that statement works

Solution 10 - Git

Since all these options only work in linux, here's the windows equivalent for anybody having to deal with that:

FOR /F usebackq %t IN (`git tag`) DO @git tag --delete %t

Solution 11 - Git

Powershell v7 supports parallel foreach if you have lots of upstream (origin) tags that you need to delete:

git tag | foreach-object -Parallel { 
git push origin --delete $_ 
git tag -d $_ 
}

Solution 12 - Git

To delete all the local tags simple run the following command

git tag | xargs git tag -d

To delete remote tags after deleting the local tags by running the above command, you can run the comand below

git ls-remote --tags --refs origin | cut -f2 | xargs git push origin --delete

NOTE: replace origin with your remote handler

Solution 13 - Git

Show all tags containing "v"

git tag -l | grep v | xargs -n 1 sh -c 'echo "Processing tag $0\n" && git show -s $0'

Solution 14 - Git

Recent version parallelized and filtered

git tag -l "v1.0.*" | xargs -L 1 | xargs git push origin --delete
git fetch origin --prune --prune-tags

First line, remove all matching tags from remote in parallel.
Second line, update the current repo by pruning all deleted tags, from git version v2.26.2.

To test the first line you can add --dry-run, I also encourage you to explore the tag list command, it has nice wildcards and exclusion/inclusion.

Solution 15 - Git

A one liner that deletes both local and remote tags with a wild card pattern.

TAGPATTERN="0.1.*" ; git push origin --delete $(git tag -l $TAGPATTERN) ; git tag -d $(git tag -l $TAGPATTERN)

Remote tags are deleted first as the list is generated from local.

Solution 16 - Git

Locally, git tags are just files on disk stored in .git/refs/tags subfolder.

You could just cd .git/refs/tags and remove all files stored there, with your favourite method of deleting files (rm *, delete from files explorer UI etc.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIonică BizăuView Question on Stackoverflow
Solution 1 - GitFlorian MargaineView Answer on Stackoverflow
Solution 2 - GitkarlingenView Answer on Stackoverflow
Solution 3 - GitRichard A QuadlingView Answer on Stackoverflow
Solution 4 - GitStefan DomnanovitsView Answer on Stackoverflow
Solution 5 - GitValtoni BoaventuraView Answer on Stackoverflow
Solution 6 - GitYong ChoiView Answer on Stackoverflow
Solution 7 - GitGigi2m02View Answer on Stackoverflow
Solution 8 - GitViZekeView Answer on Stackoverflow
Solution 9 - GitBrady HuangView Answer on Stackoverflow
Solution 10 - GitDarkWiiPlayerView Answer on Stackoverflow
Solution 11 - GitPeter Rekdal SundeView Answer on Stackoverflow
Solution 12 - Gitprince davidView Answer on Stackoverflow
Solution 13 - GitWilliam DesportesView Answer on Stackoverflow
Solution 14 - GitGiulio CaccinView Answer on Stackoverflow
Solution 15 - GitKickahaView Answer on Stackoverflow
Solution 16 - Gitjakub.gView Answer on Stackoverflow