Skip to content

Instantly share code, notes, and snippets.

@aruizca
Last active May 12, 2020 18:46
Show Gist options
  • Save aruizca/9bc2f2d8544b3b2f83dd5b0b3da7f316 to your computer and use it in GitHub Desktop.
Save aruizca/9bc2f2d8544b3b2f83dd5b0b3da7f316 to your computer and use it in GitHub Desktop.
Bash script to update tags in an existing Git repository
#!/usr/bin/env bash
commit_hashes=($(git ls-remote --tags --refs -q | awk '{ print $1 }'))
tags=($(git ls-remote --tags --refs -q | awk '{ print $2 }' | sed 's/refs\/tags\///'))
COUNTER=0
echo "Start processing ${#tags[@]} tags"
echo "================================="
for tag in "${tags[@]}"
do
hash=${commit_hashes[COUNTER]}
if [[ "${tag}" != cloud-* ]]
then
echo "Add new tag 'cloud-${tag}' to local repo on commit ${hash}"
eval "git tag cloud-${tag} ${hash}"
echo "Delete old tag '${tag}' from local"
eval "git tag --delete ${tag}"
echo "Delete old tag '${tag}' from remote"
eval "git push origin :refs/tags/${tag}"
echo "---------------------------------"
let COUNTER=COUNTER+1
fi
done
echo "Push all local tags to remote"
eval "git push origin --tags"
echo "Finished"
echo "================================="
@aruizca
Copy link
Author

aruizca commented May 12, 2020

Output sample:

Start processing 165 tags
=================================
Add new tag 'cloud-1.0' to local repo on commit d0fa426afe965a04794ea1e7921be47cfebe5888
Delete old tag '1.0' from local
Deleted tag '1.0' (was d0fa426a)
Delete old tag '1.0' from remote
---------------------------------
...
---------------------------------
Push all local tags to remote
Enumerating objects: 337, done.
Counting objects: 100% (313/313), done.
Delta compression using up to 12 threads
Compressing objects: 100% (213/213), done.
Writing objects: 100% (229/229), 31.83 KiB | 4.55 MiB/s, done.
Total 229 (delta 34), reused 173 (delta 16)
remote: Resolving deltas: 100% (34/34), completed with 22 local objects.
To ssh://example.com/whatever.git
 * [new tag]           cloud-1.0 -> cloud-1.0
....
Finished
=================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment