513

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

Attila O.
  • 14,533
  • 11
  • 53
  • 84
  • 2
    Since you seem to be good at manipulating data with pipes and whatnot, you should know about [git aliases](https://git.wiki.kernel.org/index.php/Aliases). In this case, there is a command for what you want (see answers) but eventually you will find something where there is not, and aliases are great for that. – Tyler Apr 19 '11 at 04:02
  • @[MatrixFrog](http://stackoverflow.com/users/65977/matrixfrog) thanks for the tip! I already did have some simple git aliases, but I didn't know just how powerful they can be until now. I especially like the graphviz display. – Attila O. Apr 19 '11 at 19:39
  • 1
    Huh. When I run `git describe --tags` I get the message, _"fatal: No names found, cannot describe anything."_. – Quinn Comendant Jan 28 '17 at 13:02
  • @QuinnComendant You probably need to tag something first for `--tags` to work. Try [creating a tag first](https://git-scm.com/book/en/v2/Git-Basics-Tagging); e.g. `git tag 1.0.0`. – Attila O. Jun 29 '17 at 08:19
  • Possible duplicate of [git get short hash from regular hash](https://stackoverflow.com/questions/16413373/git-get-short-hash-from-regular-hash) – Cristian Ciupitu May 09 '18 at 11:43

8 Answers8

943

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.

cambunctious
  • 6,718
  • 4
  • 30
  • 44
Mark Longair
  • 415,589
  • 70
  • 403
  • 320
  • 6
    you can do the reverse and get the long commit hash from the short commit hash by doing the following ```git rev-parse HEAD``` – Andrew Jan 12 '15 at 17:21
  • 14
    The command also works with long rev IDs that are copy-pasted from the other sources like `git log`, eg `git rev-parse --short 97dd2ae065771908ee9ae0fa08ccdb58b5a6b18f` returns `97dd2ae` – chiborg Jan 15 '16 at 14:55
  • 4
    It just works with references. You can use HEAD, tag names, branch names or plain hashes. – d12frosted Apr 04 '16 at 16:28
  • 5
    Warning, this returns a 7 character commit hash (by default) while many places like gitlab use 8 characters! – masterxilo Nov 13 '19 at 19:37
  • 28
    You can use `git rev-parse --short=8 HEAD` to get the 8 character length that is used by GitLab. You can also set `core.abbrev` to 8 for a specific git repo with a command like `git config core.abbrev 8` [Source](https://git-scm.com/docs/git-rev-parse#Documentation/git-rev-parse.txt---shortlength) – n8felton Nov 25 '19 at 13:29
  • At Ubuntu 20.04 `git rev-parse --short=7 ...` returns 8 charecters for me. even if I put `--short=6` or less - it returns 8!! – Andrey Zentavr Sep 30 '20 at 18:35
  • 2
    Also important to know is that the number you provide to `--short=n` is just a _minimum_. It may return a longer string in order to guarantee that hash points to a unique commit. From [the docs](https://git-scm.com/docs/git-rev-parse#Documentation/git-rev-parse.txt---shortlength): "[it] shortens the object name to a unique prefix with at least `length` characters". – doingweb Feb 10 '22 at 23:57
139

You can do just about any format you want with --pretty=format:

git log -1 --pretty=format:%h 
Karl Bielefeldt
  • 45,012
  • 10
  • 59
  • 90
97
git log -1 --abbrev-commit

will also do it.

git log --abbrev-commit

will list the log entries with abbreviated SHA-1 checksum.

Sanjeev
  • 1,273
  • 1
  • 12
  • 10
  • 3
    The second one is exactly what I was looking for! Very simple to remember and useful in daily life. – iFreilicht Nov 17 '17 at 11:04
  • 2
    Also works with `git log --pretty=oneline`, which unlike `--oneline`, otherwise prints full size hashes. – sdaau May 16 '20 at 16:54
  • Awesome! I added the following [alias](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases): `abbrev = log --abbrev-commit` so now I can run `git abbrev` – Michael Hall Aug 07 '20 at 01:11
62

A simple way to see the Git commit short version and the Git commit message is:

git log --oneline

Note that this is shorthand for

git log --pretty=oneline --abbrev-commit
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
The Gilbert Arenas Dagger
  • 10,985
  • 10
  • 62
  • 77
  • 2
    --oneline is the best option – Juan Ignacio Barisich Jul 23 '19 at 13:57
  • 1
    @JuanIgnacioBarisich the best option depends on how much information you need to view. In case one needs more information like author or date then git log --abbrev-commit would be a better option. also log --pretty might be a better option to choose which information to log – velocity Feb 13 '20 at 15:05
37

A really simple way is to:

git describe --always
Steven Shaw
  • 5,753
  • 2
  • 31
  • 42
  • 1
    ha, sweet, that addresses the cases where git describe will fail otherwise (because describe expects a tag somewhere in history) thx – keen May 18 '16 at 23:21
  • 10
    Not good if you strictly want the short hash - since this can return an annotated tag is there is one. – Zitrax Jun 09 '16 at 12:15
  • In some cases `git describe --long` could help. From the [docs](https://git-scm.com/docs/git-describe#Documentation/git-describe.txt---long): "Always output the long format (the tag, the number of commits and the abbreviated commit name) *even when it matches a tag*." [my emphasis] – djvg Apr 23 '20 at 19:56
  • Using `--long` is better but sometimes you get a short hash and sometimes 3 items separated by hyphens. These days, I use the accepted answer. Back in the day, I didn't know about annotated tags — perhaps they didn't even exist! – Steven Shaw Apr 23 '20 at 23:21
21

Branch with short hash and last comment:

git branch -v

  develop      717c2f9 [ahead 42] blabla
* master       2722bbe [ahead 1] bla
Attila O.
  • 14,533
  • 11
  • 53
  • 84
Fabrice
  • 307
  • 3
  • 6
16

I have Git version 2.7.4 with the following settings:

git config --global log.abbrevcommit yes
git config --global core.abbrev 8

Now when I do:

git log --pretty=oneline

I get an abbreviated commit id of eight digits:

ed054a38 add project based .gitignore
30a3fa4c add ez version
0a6e9015 add logic for shifting days
af4ab954 add n days ago
...
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Down the Stream
  • 573
  • 6
  • 10
3

what about this :

git log --pretty="%h %cD %cn %s"  

it shows someting like :

674cd0d Wed, 20 Nov 2019 12:15:38 +0000 Bob commit message

see the pretty format documentation

velocity
  • 1,400
  • 17
  • 22