KanwarG
KanwarG

Reputation: 1244

Delete multiple git remote tags and push once

In Git, how can I delete multiple tags before pushing?

I know how to do it with one tag at a time. Not sure if it's possible to do multiple.

Upvotes: 69

Views: 34350

Answers (7)

Teodorico Maziviala
Teodorico Maziviala

Reputation: 426

If you are like me and want to get the best out from your automation and time, you could make it easier by listing all the remote tags into a file sh type. with this command git ls-remote --tags <git remote repo> .

Than, in that file, replace the commit hash with the git tag delete command.

like this:

#!/bin/zsh
git push origin --delete    refs/tags/0.1.0
git push origin --delete    refs/tags/0.1.0^{}
git push origin --delete    refs/tags/0.1.1
git push origin --delete    refs/tags/0.1.10
git push origin --delete    refs/tags/0.1.100
git push origin --delete    refs/tags/0.1.101
git push origin --delete    refs/tags/0.1.102
git push origin --delete    refs/tags/0.1.103
git push origin --delete    refs/tags/0.1.104

once finished, change the file into an executable chmod +x <file-name.sh than run the file like this:

./file-name.sh

the output should look like this:

- [deleted] refs/tags/0.1.104

and this will run recursively until finished.

Upvotes: 1

Ali Karaca
Ali Karaca

Reputation: 3851

Delete everything except:

//Delete local
git tag -d $(git tag -l | grep -v "^YourTagNameToExcludeHERE*")

//Delete remote
git push -d origin $(git tag -l | grep -v "^YourTagNameToExcludeHERE*")

Extended @AechoLiu's great answer.

Upvotes: 3

Igor Rjabinin
Igor Rjabinin

Reputation: 891

if you have too many tags (like in our case) you might want to do it like:

git tag -l > tags_to_remove.txt

then edit the file in your preferred editor - to review and remove the tags you want to keep (if any) and then run it locally

git tag -d $(cat ./tags_to_remove.txt)

and remotely:

git push -d origin $(cat ./tags_to_remove.txt)

Upvotes: 53

zigarn
zigarn

Reputation: 11595

To delete locally multiple tags: git tag:

git tag -d <tagname>...

So simply:

git tag -d TAG1 TAG2 TAG3

To delete multiple tags remotely: git push:

git push [-d | --delete] [<repository> [<refspec>...]]

So simply:

git push ${REMOTE_NAME:-origin} --delete TAG1 TAG2 TAG3

TL;DR:

git tag -d TAG1 TAG2 TAG3
git push origin -d TAG1 TAG2 TAG3

Upvotes: 78

AechoLiu
AechoLiu

Reputation: 18368

It will delete all matching tag patterns.

//Delete remote:
git push -d origin $(git tag -l "tag_prefix*")

// Delete local:
git tag -d $(git tag -l "tag_prefix*")

// Examples:
git tag -d $(git tag -l "v1.0*")
git push -d origin $(git tag -l "*v3.[2]*-beta*")

Upvotes: 115

Lemtronix
Lemtronix

Reputation: 618

I found an easy way to do this if you have grep and xargs installed. I am shamelessly taking this from https://gist.github.com/shsteimer/7257245.

Delete all the remote tags with the pattern your looking for:

git tag | grep <pattern> | xargs -n 1 -I% git push origin :refs/tags/%

Delete all your local tags:

git tag | xargs -n 1 -I% git tag -d %

Fetch the remote tags which still remain:

git fetch

Upvotes: 20

Francesco
Francesco

Reputation: 4250

You can delete multiple tags with one command by specifying all the tags you want to delete

git tag -d 1.1 1.2 1.3

Then you can push all the deleted tags. Of course you can delete the tags with separate commands before pushing.

To push delete tags, just list all the tags you want to delete. The command is the same to delete one tag

git push --delete origin 1.1 1.2 1.3

Upvotes: 7

Related Questions