Remove local git tags that are no longer on the remote repository

This is great question, I’d been wondering the same thing.

I didn’t want to write a script so sought a different solution. The key is discovering that you can delete a tag locally, then use git fetch to “get it back” from the remote server. If the tag doesn’t exist on the remote, then it will remain deleted.

Thus you need to type two lines in order:

git tag -l | xargs git tag -d
git fetch --tags

These:

  1. Delete all tags from the local repo. FWIW, xargs places each tag output by “tag -l” onto the command line for “tag -d”. Without this, git won’t delete anything because it doesn’t read stdin (silly git).

  2. Fetch all active tags from the remote repo.

This even works a treat on Windows.

Leave a Comment