5

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
Alon
  • 10,381
  • 23
  • 88
  • 152
  • 2
    Shouldn't you do your own homework? (If it's not a homework question, it really needs a lot of rephrasing...) – torek Aug 03 '20 at 05:23
  • 4
    @torek I'm not asking for myself. I already know the answer. I've just seen that there is no question on SO regarding this specific command, and it could help programmers if such a question existed. – Alon Aug 03 '20 at 05:25
  • 1
    If you know the answer, you should also post it... – Philippe Aug 03 '20 at 08:59
  • 1
    I doubt there is a point in copy-pasting the documentation for every git command or whatnot onto Stack Overflow camoflaged as a question. They had a documentation part of the site previously, but it was decomissioned. – Lasse V. Karlsen Aug 03 '20 at 10:33
  • Does this answer your question? [What does git rev-parse do?](https://stackoverflow.com/questions/15798862/what-does-git-rev-parse-do) – Lasse V. Karlsen Aug 03 '20 at 10:33
  • Also, what is HEAD is a separate question from what git rev-parse does, please don't post more than one question at a time. – Lasse V. Karlsen Aug 03 '20 at 10:34

1 Answers1

8

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

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.

Philippe
  • 28,207
  • 6
  • 54
  • 78
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250