Skip to content

Instantly share code, notes, and snippets.

@yan-foto
Created April 23, 2018 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yan-foto/c1f57bf1fe4f63d4f674d98e96e23730 to your computer and use it in GitHub Desktop.
Save yan-foto/c1f57bf1fe4f63d4f674d98e96e23730 to your computer and use it in GitHub Desktop.
Delete all git tags (remote and local) older than a given date [Linux/MacOS]
#!/bin/sh
# Make sure date is given
if [ $# -eq 0 ]
then
printf "Usage:\n bash git-del-tag.sh 2018-04-23"
exit 1
fi
# date command has different syntax for Linux and Mac
uname=$(uname)
case "$uname" in
(*Linux*) dateCmd='date +%s -d' ;;
(*Darwin*) dateCmd='date -j -f "%Y-%m-%d" +%s' ;;
esac
delRemoteCmd='git push --delete origin'
delLocalCmd='git fetch --prune origin "+refs/tags/*:refs/tags/*"'
# User given date to epoch timestamp
userTS=`eval "$dateCmd $1"`
git for-each-ref --sort=taggerdate --format 'tag=%(refname:short); date=%(taggerdate:short)' refs/tags |
{
# Array of matching tags
tags=()
echo "Finding matching tags..."
while read entry
do
eval "$entry"
tagTS=`eval "$dateCmd $date"`
if [ $userTS -ge $tagTS ];
then
tags[${#tags[*]}]="$tag"
fi
done
# If no tags has been found just exit!
if [ ${#tags[@]} -eq 0 ]
then
echo "No matching tags found. Quitting!"
exit 0
fi
echo "Found ${tags[@]} tags..."
echo "Following tags will be removed"
echo "${tags[@]}"
read -p "Are you sure you want to continue? [N/y]" docontinue < /dev/tty
if [ "$docontinue" = "y" ];
then
echo "Removing remote tags..."
eval "$delRemoteCmd ${tags[@]}"
echo "Purging local tags..."
eval "$delLocalCmd"
fi
}
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment