Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all lightweight tags?

Tags:

I want to list all of the lightweight tags in my repository; the best I can think of involves combining git for-each-ref, grep, and cut, but it seems like it'll be kind of fiddly...

(While we're at it, we might as well talk about the same thing for annotated tags: someone is sure to end up here wondering about that at some point.)

Edit:

By lightweight tags, I meant those tag refs that do not refer to tag objects. (In other words, unannotated tags.)

like image 750
SamB Avatar asked Jan 09 '14 21:01

SamB


People also ask

How do I list all tags?

In order to list Git tags, you have to use the “git tag” command with no arguments. You can also execute “git tag” with the “-n” option in order to have an extensive description of your tag list. Optionally, you can choose to specify a tag pattern with the “-l” option followed by the tag pattern.

How do you check if a tag is lightweight or annotated?

Get the tag name (say foo ) and then do a git cat-file -t foo . If it's an an annotated tag, cat-file will tell you that it's a "tag".

How do I see tags in git?

To view information about Git tags in a CodeCommit repository. Run the git ls-remote --tags command. If no Git tags have been defined, git ls-remote --tags returns a blank line.

Which command correctly creates a lightweight tag?

Sharing Tagsgit push <remote> --tags will push both lightweight and annotated tags. There is currently no option to push only lightweight tags, but if you use git push <remote> --follow-tags only annotated tags will be pushed to the remote.


1 Answers

All the lightweight tags are in the refs/tags/ namespace and can be enumerated with, e.g.:

git for-each-ref --format '%(refname:short)' refs/tags/ 

or:

git show-ref --tags 

As for annotated tags, well, the trick here—it affects the "lightweight" tags part too—is that an annotated tag is actually an object in the git repository, but, there's a lightweight tag that points to that object, that lets you get at the annotated tag by its tag name.1 So it's really a pair of things: a lightweight tag, plus the in-repo annotated tag object, that makes it "not a lightweight tag", except for that stubborn fact that it is a lightweight tag at the same time!

Thus, it boils down to: find all lightweight tags, then optionally select only tags pointing to commits or tags pointing to tag-objects depending on the behavior you want, then go on to emit the tag name.

There's a long example in the git-for-each-ref documentation of writing an entire script in the --format string and using eval to execute it (or you could pipe to sh to execute, at the cost of one extra process). I usually find it simpler to pipe the output of git for-each-ref into a while read ... loop:

git for-each-ref refs/tags/ --format '%(objecttype) %(refname:short)' |     while read ty name; do [ $ty = commit ] && echo $name; done 

which prints all lightweight-only tags.

Compare with:

git for-each-ref refs/tags/ --format '%(objecttype) %(refname:short)' |     while read ty name; do [ $ty = tag ] && echo $name; done 

which prints all annotated tags (or more precisely, "lightweight-that-are-annotated" tags).

Note that a tag can (conceivably—there's no actual use case for this right now, as far as I know) point to something other than a commit or a tag; it's up to you whether to do something with a tag pointing directly to a tree or blob.


1Without the lightweight tag, you would not be able to refer to annotated tag annotag using the name annotag—not without going through all the search effort that git fsck uses to find dangling objects, at least. Moreover, if you delete the lightweight tag, the annotated tag object may get garbage-collected. You can make one tag object point to another tag object to keep it in the repo (i.e., inhibit the gc) without an external name for the second tag object, as long as the first one has an external name. That's definitely an odd thing to do though.

Interestingly, the internal format for the annotated tag contains the external name, so one can use this technique to protect "old" annotated tags, hide them by removing their lightweight tags, and then later restore the original lightweight tag. Whether anyone can come up with a use for this, though... :-)

like image 73
torek Avatar answered Oct 30 '22 10:10

torek