- Published on
Remove all local GitHub tags which aren't on remote
My teammate have manually deleted 40+ tags in remote GitHub repository. Since GitHub doesn't have a notion of source controlld tags, if you were to do push another tag from your local machine which doesn't have those tags removed, you would end-up reconstructing all these removed tags.
There is no easy way to just sync all the remote & local tags in GitHub CLI, so here is something what will save you bunch of time (or the need to manually delete tag by tag).
The script below loops through your local tags, and will remote those not present in remote.
local_tag_count=$(git tag | wc -l)
remote_tag_count=$(git ls-remote --tags | wc -l)
echo "Local tag count: ${local_tag_count}"
echo "Remote tag count: ${remote_tag_count}"
remote_tags=$(git ls-remote --tags)
local_tags=$(git tag)
while IFS= read -r local_tag; do
echo "Checking local tag: '$local_tag'"
if [[ ! "${remote_tags[*]}" =~ "$local_tag" ]]; then
echo "Local tag doesn't exist in remote. Removing..."
git tag -d $local_tag
break # Uncomment this line if you want to update all tags
fi
done <<<"$local_tags"
tag_count=$(git tag | wc -l)
echo "Local tags before: $local_tag_count"
echo "Local tags after: $tag_count"
echo "Remote tag count: ${remote_tag_count}"
P.S: I put 'break' there in code on purpose. So that if you run it accidentally it will only delete 1 local tag. Feel free to uncommnet it after you run it once and are happy with the outcome, or simply if you feel brave.
Have fun!