github - get git hash for commit with specific tag without cloning or pulling

StackOverflow https://stackoverflow.com/questions/23677304

  •  23-07-2023
  •  | 
  •  

Question

I wish to get the git hash for a specific tag. For now what I do on the job is:

git clone GITHUB_URL
git checkout TAG
cd REPO_NAME
export GIT_HASH=$(git log --pretty=format:%h -1)

Is there any chance I can get this without cloning or pulling or checking out?

Était-ce utile?

La solution 2

so I started out with what @hvd said:

git ls-remote REPOLINK TAG

but that gave me only the tag hash, and I want the commit hash.

so I went back to:

git ls-remote REPOLINK 

and decided to grep for the tag:

git ls-remote REPOLINK | grep TAG

so now I have 2 rows, the first with the tag hash, and the 2nd with commit hash. so first we need to take just the second line:

git ls-remote REPOLINK | grep TAG | sed -n 2p

and we now have:

HASH TAG

now lets cut off everything but the first column to get the hash:

git ls-remote REPOLINK | grep TAG | sed -n 2p | cut -f1

Autres conseils

You can use git ls-remote for this. An example:

$ git ls-remote git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
14186fea0cb06bc43181ce239efe0df6f1af260a        HEAD
14186fea0cb06bc43181ce239efe0df6f1af260a        refs/heads/master
5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c        refs/tags/v2.6.11
c39ae07f393806ccf406ef966e9a15afc43cc36a        refs/tags/v2.6.11^{}
5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c        refs/tags/v2.6.11-tree
c39ae07f393806ccf406ef966e9a15afc43cc36a        refs/tags/v2.6.11-tree^{}
26791a8bcf0e6d33f43aef7682bdb555236d56de        refs/tags/v2.6.12
9ee1c939d1cb936b1f98e8d81aeffab57bae46ab        refs/tags/v2.6.12^{}
9e734775f7c22d2f89943ad6c745571f1930105f        refs/tags/v2.6.12-rc2
1da177e4c3f41524e886b7f1b8a0c1fc7321cac2        refs/tags/v2.6.12-rc2^{}
0397236d43e48e821cce5bbe6a80a1a56bb7cc3a        refs/tags/v2.6.12-rc3
a2755a80f40e5794ddc20e00f781af9d6320fafb        refs/tags/v2.6.12-rc3^{}
ebb5573ea8beaf000d4833735f3e53acb9af844c        refs/tags/v2.6.12-rc4
88d7bd8cb9eb8d64bf7997600b0d64f7834047c5        refs/tags/v2.6.12-rc4^{}
06f6d9e2f140466eeb41e494e14167f90210f89d        refs/tags/v2.6.12-rc5
2a24ab628aa7b190be32f63dfb6d96f3fb61580a        refs/tags/v2.6.12-rc5^{}
701d7ecec3e0c6b4ab9bb824fd2b34be4da63b7e        refs/tags/v2.6.12-rc6
7cef5677ef3a8084f2588ce0a129dc95d65161f6        refs/tags/v2.6.12-rc6^{}
0da688d20078783b23f99b232b272b027d6c3f59        refs/tags/v2.6.13
02b3e4e2d71b6058ec11cc01c72ac651eb3ded2b        refs/tags/v2.6.13^{}
...

You can also ask only for the specific ref you want:

$ git ls-remote git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git HEAD
14186fea0cb06bc43181ce239efe0df6f1af260a        HEAD

Per the comments, if annotated tags are used, asking for the tag will get you the hash of the tag object, not the associated commit. The associated commit is available as <tag ref>^{}:

$ git ls-remote git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git refs/tags/v3.14
f2378b14895ac79c325abe3c933744a26465e570        refs/tags/v3.14
$ git ls-remote git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git refs/tags/v3.14^{}
455c6fdbd219161bd09b1165f11699d6d73de11c        refs/tags/v3.14^{}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top