How to delete local and remote tags massively from a git repo in just one line
TL;DR. Give me the magic 🧙♂️
WARNING! This is a destructive command. Use at your own risk
Use case: you want to delete the major version 0 and its children.
First, a dry run…
SEMVER=0 && git tag | awk "/^$SEMVER.\*/ { print \\$1 }"
This way you see what you’re about to delete.
Now, if you are certain, then bring the chaos…
SEMVER=0 && git tag | awk "/^$SEMVER.\*/ { print \\$1 }" | xargs -I % sh -c "git push origin :%; git tag -d %;"
Notes
The SEMVER variable is a regular expression used inside the awk command. So if you would like to delete only the 0.1 version and its patches (0.1.1, 0.1.2, etc.), you could use SEMVER=0.1
in the command.
Windows users: you can still use GitBash.
I want the details 🤓
Let’s see the entire command again for deleting major version 0 and its children.
SEMVER=0 && git tag | awk "/^$SEMVER.\*/ { print \\$1 }" | xargs -I % sh -c "git push origin :%; git tag -d %;"
This one-line-command actually does the following things:
SEMVER=0
Declaring the SEMVER variable and for later use inside the awk command.git tag
Showing the available local tags in our local git repo.awk “/^$SEMVER.*/ { print \$1 }”
Here we use awk for filtering all the matching versions with the regular expression defined in the SEMVER variable.xargs -I % sh -c "git push origin :%; git tag -d %;"
Then, we use xargs to use the input through the percentage char (%) and combine it with the sh command.
We tell to sh to read the execution from a string thanks to the-c
argument.
Finally, inside the string we execute the git commands to delete the tag remotely first, and then locally as well. Notice we’re using the xargs’ input through the percentage char (%) here.
Hope it helps!
Thanks for your time!
I'm nahuelhds. If you like what I do, you can...