Search code examples
gitgit-pushgit-tag

How to push all annotated tags?


How do I push all annotated tags from my local repo to the remote, without pushing lightweight tags?

I use lightweight tags locally that I don't want pushed, so git push --tags is not a solution.

I am aware of git push --follow-tags but that will only push tags that are associated with commits currently being pushed.

I need this option because my normal workflow involves pushing from time to time, but only tagging when finalizing a release, and I sometimes forget to push a tag. This problem often goes undetected for a long time and causes a bit of confusion with other developers, since we become out of sync with our tags.

Since it's complicated to list which annotated tags exist locally but not remotely, I would like to solve this problem by just pushing all local annotated tags, which will ensure that all developers' local and remote repos have the same annotated tags.


Solution

  • It's not too difficult. First find all annotated tags (by rejecting tags that point directly to commits rather than to annotated tag objects). This is a bit long, so you might wish to write it as a shell function:

    list_annotated_tags() {
        git for-each-ref --format '%(objecttype) %(refname)' refs/tags |
            while read reftype refname; do
                case $reftype in tag) echo $refname;; esac
            done
    }
    

    Read the output of the above and use them as refspec arguments to a git push command:

    git push origin $(list_annotated_tags)
    

    and your script is complete.