Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rev-parse --short HEAD

Tags:

git

What does the following command do?:

git rev-parse --short HEAD

The answer should include:

  1. A short and to the point answer that explains what this command does as a whole
  2. Explanation of rev-parse
  3. Explanation of --short
  4. Explanation of HEAD
like image 881
Alon Avatar asked Sep 17 '25 05:09

Alon


1 Answers

git rev-parse --short is both a way to:

  • verify that it can be turned into a raw 20-byte SHA-1 that can be used to access the object database
  • get an abbreviated SHA (SHA1 for now, SHA2 later on), respecting core.abbrev setting (as I mention here), 7 characters by default.
    But if 7 characters is not enough to produce a unique SHA, git rev-parse will generate the right length.

Applied to HEAD, it gets back a short version of the SHA1 of the currently checked out commit (which is not necessarily a branch, when the HEAd is detached)

You can see it used for the first time in the git/git code base itself in commit e3ae4a8613151c93ffce78c674ac91c1ee34eef6, Aug. 2009, Git v1.6.5-rc0.

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

So instead of:

${sub1sha1:0:7}

Use:

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

You find it used then in commit e8f21ca, June 2013, Git v1.8.4-rc0, for the bash prompt:

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.

Because --short will compute the minimum lenght for a SHA to be non-ambiguous, it should be the last option used by git rev-parse.

See 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.

like image 190
VonC Avatar answered Sep 19 '25 21:09

VonC