Sahil Lakhwani
Sahil Lakhwani

Reputation: 108

How to get git tag for a commit hash in a remote repository?

You can get the tag which points at a particular commit in your local repository by doing this:

git tag --points-at <commit-hash>

or this:

git describe --exact-match <commit-hash>

Is this possible for a remote repository too, without even cloning the repository?

Upvotes: 4

Views: 5205

Answers (1)

ElpieKay
ElpieKay

Reputation: 30938

git ls-remote -t <remote> | grep <commit-hash>

git ls-remote lists all the refs and their sha1 values in the remote repository. -t limits to tags only. If you are under a local git repository, and the remote is origin, <remote> can be omitted. You can run the command anywhere if you specify <remote> like https://github.com/foo/bar.git. -t must come before <remote> if <remote> exists in the command. See more at git-ls-remote.

Update:

No, there is not something like --points-at for git ls-remote. If you know a tag name, git ls-remote <remote> <tag_name> returns the sha and the tag, but not possible from a sha to a tag name.

There are two types of tags. One is a lightweight tag, and the other is an annotated tag. The formmer is only a ref and the latter is a git object. Git has four kinds of objects, commit, tag, tree and blob.

If v1.0 is a lightweight tag, v1.0 and v1.0^{} are the same.

If v1.0 is an anotated tag, v1.0 is a tag object and v1.0^{} is the commit it refers to. As an anotated tag, v1.0 and v1.0^{} are the same for many git commands, for example git log, git show, git diff, when they are resolved as commit or tree. In git log v1.0, v1.0 is a commit-ish. As a commit-ish, v1.0 and v1.0^{} refer to the same commit. So we get the same output from git log v1.0 and git log v1.0^{}. For other git commands like git rev-parse, they are different. In git rev-parse -t v1.0, v1.0 is a tag object. And in git rev-parse -t v1.0^{}, v1.0^{} is a commit object.

Upvotes: 7

Related Questions