Ionică Bizău
Ionică Bizău

Reputation: 113345

Delete all tags from a Git repository

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.

Upvotes: 372

Views: 134678

Answers (20)

xinyu
xinyu

Reputation: 138

You can use those command lines to delete both local and remote tags.

#Delete local tags.
git tag -d $(git tag -l)
#Fetch remote tags.
git fetch
#Delete remote tags.
git push origin --delete $(git tag -l) # Pushing once should be faster than multiple times
#Delete local tags.
git tag -d $(git tag -l)

Upvotes: 1

David White
David White

Reputation: 1

In PowerShell you can just run this:

git tag | % {git tag -d $_}

Upvotes: 0

Mehdi Hadeli
Mehdi Hadeli

Reputation: 10174

For windows, doing this for removing remote tags:

git fetch --tags
git push origin --delete $(git tag -l)

Upvotes: 2

David Mukopa
David Mukopa

Reputation: 277

To delete all the local tags simply 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

Upvotes: 20

Markus Ressel
Markus Ressel

Reputation: 1135

I didn't find a solution anywhere that didn't requre a 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 tags
    • git 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 unwanted refs/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.

Upvotes: 1

Brady Huang
Brady Huang

Reputation: 2002

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

Upvotes: 8

Giulio Caccin
Giulio Caccin

Reputation: 3052

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.

Upvotes: 2

jakub.g
jakub.g

Reputation: 41248

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.)

Upvotes: 0

Kickaha
Kickaha

Reputation: 3857

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.

Upvotes: 1

Florian Margaine
Florian Margaine

Reputation: 60717

git tag | xargs git tag -d

Simply follow the Unix philosophy where you pipe everything.

On Windows use git bash with the same command.

Upvotes: 627

William Desportes
William Desportes

Reputation: 1701

Show all tags containing "v"

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

Upvotes: 1

Peter Rekdal Sunde
Peter Rekdal Sunde

Reputation: 121

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 $_ 
}

Upvotes: 3

DarkWiiPlayer
DarkWiiPlayer

Reputation: 7046

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

Upvotes: 3

karlingen
karlingen

Reputation: 14625

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

Upvotes: 344

Valtoni Boaventura
Valtoni Boaventura

Reputation: 1607

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.

Upvotes: 37

ViZeke
ViZeke

Reputation: 359

You can also use:

git tag -d $(git tag)

Upvotes: 11

Yong Choi
Yong Choi

Reputation: 331

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 $_ }  

Upvotes: 33

Richard A Quadling
Richard A Quadling

Reputation: 3988

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.

Upvotes: 105

Stefan Domnanovits
Stefan Domnanovits

Reputation: 631

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.

Upvotes: 53

Gigi2m02
Gigi2m02

Reputation: 1258

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)

Upvotes: 15

Related Questions