github - 获取带有特定标记的提交的git哈希,无需克隆或提取

Gil*_*ner 4 git github

我希望得到特定标签的git哈希.现在我在工作中做的是:

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

如果没有克隆,拉扯或退房,我有没有机会得到这个?

小智 5

你可以用git ls-remote它.一个例子:

$ 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^{}
...

您也可以只询问您想要的具体参考:

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

根据注释,如果使用带注释的标签,请求标签将获得标签对象的哈希值,而不是关联的提交.关联的提交可用作<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^{}


Gil*_*ner 2

所以我从@hvd 说的开始:

git ls-remote REPOLINK TAG
Run Code Online (Sandbox Code Playgroud)

但这只给了我标签哈希,而我想要提交哈希。

所以我回到:

git ls-remote REPOLINK 
Run Code Online (Sandbox Code Playgroud)

并决定 grep 查找标签:

git ls-remote REPOLINK | grep TAG
Run Code Online (Sandbox Code Playgroud)

所以现在我有两行,第一行带有标签哈希,第二行带有提交哈希。所以首先我们只需要第二行:

git ls-remote REPOLINK | grep TAG | sed -n 2p
Run Code Online (Sandbox Code Playgroud)

现在我们有:

HASH TAG
Run Code Online (Sandbox Code Playgroud)

现在让我们切断除第一列之外的所有内容以获取哈希值:

git ls-remote REPOLINK | grep TAG | sed -n 2p | cut -f1
Run Code Online (Sandbox Code Playgroud)