gpt4 book ai didi

git rev-parse --short HEAD

转载 作者:行者123 更新时间:2023-12-05 01:11:24 31 4
gpt4 key购买 nike

以下命令的作用是什么?

git rev-parse --short HEAD

答案应该包括:

  1. 一个简短而中肯的答案,解释这个命令作为一个整体的作用
  2. rev-parse的解释
  3. --short 的解释
  4. HEAD的解释

最佳答案

git rev-parse --short都是一种方式:

应用于 HEAD,它返回当前 checkout 提交的 SHA1 的简短版本(不一定是分支,when the HEAd is detached)

您可以在 commit e3ae4a8613151c93ffce78c674ac91c1ee34eef6, Aug. 2009, Git v1.6.5-rc0 中的 git/git 代码库中看到它的首次使用。 .

The substring expansion notation is a bashism that we have not so faradopted.
Use 'git rev-parse --short' instead, as this also handles the case where the unique abbreviation is longer than 7 characters.

所以代替:

${sub1sha1:0:7}

用途:

sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD)
$sub1sha1_short

您会发现它在 commit e8f21ca, June 2013, Git v1.8.4-rc0 中使用过, 对于 bash 提示:

bash prompt: print unique detached HEAD abbreviated object name

When describing a detached HEAD according to the $GIT_PS1_DESCRIBE environment variable fails, __git_ps1() runs 'cut -c1-7 .git/HEAD' to show the 7 hexdigits abbreviated commit object name in the prompt.
Obviously, this neither respects core.abbrev nor produces a unique object name.

Fix this by using 'git rev-parse --short HEAD' instead and adjust the corresponding test to use non-standard number of hexdigits.

因为 --short 将计算 SHA 的最小长度是无歧义的,所以它应该是 git rev-parse 使用的最后一个选项。

commit e3e0b93, also June 2013, Git v1.8.4-rc0

bash prompt: combine 'git rev-parse' for detached head

When describing a detached HEAD according to the $GIT_PS1_DESCRIBE environment variable fails, __git_ps1() now runs the '$(git rev-parse --short HEAD)' command substitution to get the abbreviated detached HEAD commit object name.
This imposes the overhead of fork()ing a subshell and fork()+exec()ing a git process.

Avoid this overhead by combining this command substitution with the"main" 'git rev-parse' execution for getting the path to the .git directory & co.
This means that we'll look for the abbreviated commit object name even when it's not necessary, because we're on a branch or the detached HEAD can be described.
It doesn't matter, however, because once 'git rev-parse' is up and running to fulfill all those other queries, the additional overhead of looking for the abbreviated commit object name is not measurable because it's lost in the noise.

There is a caveat, however, when we are on an unborn branch, because in that case HEAD doesn't point to a valid commit, hence the query for the abbreviated commit object name fails.
Therefore, '--short HEAD' must be the last options to 'git rev-parse' in order to get all the other necessary information for the prompt even on an unborn branch.
Furthermore, in that case, and in that case only, 'git rev-parse' doesn't output the last line containing the abbreviated commit object name, obviously, so we have to take care to only parse it if 'git rev-parse' exited without any error.

关于git rev-parse --short HEAD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63224118/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com