Detecting all tags that are out of sync between local and remote

Viewed 148

Is there an easy way to determine which tags I have in my local repo that are out of sync with the remote? By out of sync I mean that the exact same tag name points to a different commit on my local vs the remote. Two ways I can think of to end up in this situation might be:

  1. Someone (or something) moved a tag that I previously fetched. Perhaps it was deleted and recreated, or it was force created when it already existed. (I realize this is frowned upon but that doesn't stop it from happening.)
  2. I created a tag locally, and then someone (or something) created the same tag name on a different commit and pushed it out before I did.

Another way to word this question might be:

Which of my local tags will be updated if I were to run the following command:

git fetch origin --tags --force

Update: based on the answer, this one liner should work in Git Bash:

diff <(git for-each-ref refs/tags --format="%(objectname)%09%(refname)") <(git ls-remote --tags | grep -v "\^{}")

Explanation: The command lists all your local tags, and formats it so that it uses the same format as ls-remote. (%09 is a tab character.) Then diff the two outputs.

1 Answers

"Easy" is in the eye of the beholder: Use git ls-remote --tags to dump out the tags in the remote, and git for-each-ref refs/tags to inspect your local tags. Compare the hash IDs of the tags (in the ls-remote output, this is the number that does not have the ^{} suffix; the one with the suffix, if there is one, is the hash ID of the tag's ultimate target). When the names match but the hash IDs differ, they're out of sync. It would be relatively simple to write a shell script for this.

Related