DEV Community

Nahue
Nahue

Posted on • Originally published at nahuelhds.dev on

How to delete local and remote tags massively from a git repo in just one line

Resultado de imagen para git

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 }"
Enter fullscreen mode Exit fullscreen mode

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 %;"
Enter fullscreen mode Exit fullscreen mode

Thanos is so proud of youThanos is so proud of you

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 %;"
Enter fullscreen mode Exit fullscreen mode

This one-line-command actually does the following things:

  1. SEMVER=0 Declaring the SEMVER variable and for later use inside the awk command.
  2. git tag Showing the available local tags in our local git repo.
  3. awk “/^$SEMVER.*/ { print \$1 }” Here we use awk for filtering all the matching versions with the regular expression defined in the SEMVER variable.
  4. 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!

Top comments (1)

Collapse
 
bjornharrtell profile image
Björn Harrtell

Great stuff, thanks! :)