如何在 Git 中找到分支的哈希值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9110478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:53:17  来源:igfitidea点击:

How to find the hash of branch in Git?

git

提问by Misha Moroshko

Given a local / remote branch name, how could I get the hash of the commit that this branch points to?

给定一个本地/远程分支名称,我如何获得该分支指向的提交的哈希值?

回答by Mark Longair

The command git rev-parseis your friend, e.g.:

命令git rev-parse是你的朋友,例如:

$ git rev-parse development
17f2303133734f4b9a9aacfe52209e04ec11aff4

... or for a remote-tracking branch:

... 或者对于远程跟踪分支:

$ git rev-parse origin/master
da1ec1472c108f52d4256049fe1f674af69e785d

This command is generally very useful, since it can parse any of the ways of specifying branch names in git, such as:

这个命令通常非常有用,因为它可以解析任何指定分支名称的方式git,例如:

git rev-parse master~3
git rev-parse HEAD@{2.days.ago}

... etc.

... 等等。

回答by Mark Fisher

The hashes are stored under .git/refs/, e.g. .git/refs/heads/master

散列存储在.git/refs/,例如.git/refs/heads/master

But programatically use git rev-parseas suggested by Mark Longair as it's safer.

但是git rev-parse按照 Mark Longair 的建议以编程方式使用,因为它更安全。

回答by VonC

Don't forget that since Git 2.19 (Q2 2018), Git prepares a transition from SH1 hashes to SHA2: see "Why doesn't Git use more modern SHA?"

不要忘记,自 Git 2.19(2018 年第二季度)以来,Git 准备从 SH1 哈希转换为 SHA2:请参阅“为什么 Git 不使用更现代的 SHA?

With Git 2.25 (Q1 2020), git rev-parseevolves and reflects that possible new hash.

在 Git 2.25(2020 年第一季度)中,git rev-parse进化并反映了可能的新哈希。

See commit fa26d5e, commit cf02be8, commit 38ee26b, commit 37ab8eb, commit 0370b35, commit 0253e12, commit 45e2ef2, commit 79b0edc, commit 840624f, commit 32a6707, commit 440bf91, commit 0b408ca, commit 2eabd38(28 Oct 2019), and commit 1bcef51, commit ecde49b(05 Oct 2019) by brian m. carlson (bk2204).
(Merged by Junio C Hamano -- gitster--in commit 28014c1, 10 Nov 2019)

提交fa26d5e提交cf02be8提交38ee26b提交37ab8eb提交0370b35提交0253e12提交45e2ef2提交79b0edc提交840624f提交32a6707提交440bf91提交0b408ca提交2eabd38(2019年10月28日),以及提交1bcef51提交ecde49b(2019 年 10 月 5 日)作者:brian m。卡尔森 ( bk2204)
(由Junio C gitsterHamano合并-- --提交 28014c1 中, 2019 年 11 月 10 日)

rev-parse: add a --show-object-formatoption

Signed-off-by: brian m. carlson

Add an option to print the object format used for input, output, or storage.
This allows shell scripts to discover the hash algorithm in use.

Since the transition plan allows for multiple input algorithms, document that we may provide multiple results for input, and the format that the results may take.
While we don't support this now, documenting it early means that script authors can future-proof their scripts for when we do.

rev-parse: 添加一个--show-object-format选项

签字人:brian m. 卡尔森

添加一个选项来打印用于输入、输出或存储的对象格式。
这允许 shell 脚本发现正在使用的哈希算法。

由于转换计划允许多个输入算法,记录我们可以为输入提供多个结果,以及结果可能采用的格式。
虽然我们现在不支持这一点,但尽早记录它意味着脚本作者可以在我们这样做时对他们的脚本进行未来验证。

The git rev-parsedocumentationnow includes:

git rev-parse文档现在包括:

--show-object-format[=(storage|input|output)]:

Show the object format (hash algorithm) used for the repository for storage inside the .gitdirectory, input, or output. For input, multiple algorithms may be printed, space-separated. If not specified, the default is "storage".

--show-object-format[=(storage|input|output)]:

显示用于存储在.git目录、输入或输出中的存储库的对象格式(哈希算法)。对于输入,可以打印多个算法,以空格分隔。如果未指定,则默认为“存储”。