Ben Lings
Ben Lings

Reputation: 29423

How to list unpushed Git tags

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?

Upvotes: 35

Views: 11622

Answers (3)

M3M0RYKNI6HT
M3M0RYKNI6HT

Reputation: 21

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 " " -`

Upvotes: 2

SRombauts
SRombauts

Reputation: 438

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)\""

Upvotes: 2

Ben Lings
Ben Lings

Reputation: 29423

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.

Upvotes: 52

Related Questions