Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the short Git version hash

Tags:

git

Is there a cleaner way to get the short version hash of HEAD from Git?

I want to see the same output as I get from:

 git log -n 1 | head -n 1 | sed -e 's/^commit //' | head -c 8 

I originally used the above command to generate a version string, but this is even better:

git describe --tags 

It will output strings like 0.1.12 (tagged commit) or 0.1.11-5-g0c85fbc (five commits after the tag).

like image 305
Attila O. Avatar asked Apr 17 '11 15:04

Attila O.


People also ask

What is git short hash?

The short hash is just a shorter version of the original (long) hash. The original Git hash is 40 bytes long while short is only 8 bytes long. However, it becomes difficult to manage it (in terms of using typing or displaying), and that's why the short version is used.

What is a short Sha?

short-sha is a GitHub Action than provides an output sha with the shortened commit SHA. ⚠️ On November 16th, 2020 Github removes the set-env command. Version prior to v1. 2 of this action will not work.

How do I get git hash from GitHub?

To search for a hash, just enter at least the first 7 characters in the search box. Then on the results page, click the "Commits" tab to see matching commits (but only on the default branch, usually master ), or the "Issues" tab to see pull requests containing the commit.


1 Answers

Try this:

git rev-parse --short HEAD 

The command git rev-parse can do a remarkable number of different things, so you'd need to go through the documentation very carefully to spot that though.

like image 189
Mark Longair Avatar answered Sep 29 '22 13:09

Mark Longair