31

I'd like to see which tags I have locally that aren't available on a particular remote. How can I do this? I know I can do git push --tags to push all of them. However, if there are some tags that I don't want pushed, how to I make sure I haven't missed some?

3 Answers 3

46

You can use the following to see the tags that exist locally but not in the specified remote:

git show-ref --tags | grep -v -F "$(git ls-remote --tags <remote name> | grep -v '\^{}' | cut -f 2)"

Note that git ls-remote shows both the annotated tag and the commit it points to with ^{}, so we need to remove the duplicates.

An alternative is to use the --dry-run/-n flags to git push:

git push --tags --dry-run

This will show what changes would have been pushed, but won't actually make these changes.

2
  • 2
    Since you're using the output of git ls-remote to filter out from the show-ref, there's not much harm in leaving the ^{} lines in. That leaves us with a slightly simpler: git show-ref --tags | grep -v -F "$(git ls-remote --tags origin | cut -f 2)"
    – phinze
    Apr 10, 2013 at 16:13
  • 3
    This is the exact same command except with <remote name> replaced with origin. Use this to copy/paste: git show-ref --tags | grep -v -F "$(git ls-remote --tags origin | grep -v '\^{}' | cut -f 2)"
    – funroll
    Feb 4, 2014 at 18:11
2

For the record, I am using a variant of this with the 'comm' command:

comm -23 <(git show-ref --tags | cut -d ' ' -f 2) <(git ls-remote --tags origin | cut -f 2)

I am using it as a git alias in .gitconfig, with proper bash quoting like this:

[alias]
    unpushed-tags = "!bash -c \"comm -23 <(git show-ref --tags | cut -d ' ' -f 2) <(git ls-remote --tags origin | cut -f 2)\""
1
  • comm -23 <(git ls-remote --tags .) <(git ls-remote --tags origin). You can specify repos by local path or url or by git's extended url-style transport methods.
    – jthill
    Jan 2, 2022 at 21:36
1

I found that the accepted answer from Ben Lings missed unpushed tags which partially matched remote tags; for example unpushed tag "snowball" would not be listed if there was a remote tag called "snowba" or "snow".

I made a version that checks for exact name matches between local tags in the currently checked out branch and tags in the remote repo to find unpushed tags:

comm -23 <(echo "$(git tag --list)") <(echo "$(git ls-remote --tags -q | grep -v '\^{}' | cut -f 2 | cut -d '/' -f 3-)") | paste -s -d " " -

And if you only want to check for unpushed tags in the currently checked out branch:

comm -23 <(echo "$(git tag --merged)") <(echo "$(git ls-remote --tags -q | grep -v '\^{}' | cut -f 2 | cut -d '/' -f 3-)") | paste -s -d " " -

Here that same query for unpushed tags in the current branch is spilt up into multiple statements for use in a bash script (and for increased clarity):

local_tags_in_current_branch="$(git tag --merged)"
remote_tags="$(git ls-remote --tags -q | grep -v '\^{}' | cut -f 2 | cut -d '/' -f 3-)"
unpushed_tags=`comm -23 <(echo "$local_tags_in_current_branch") <(echo "$remote_tags") | paste -s -d " " -`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.