584

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.
  • 15,659
  • 11
  • 54
  • 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

9 Answers9

1061

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
  • 8,391
  • 5
  • 34
  • 53
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 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
  • 19
    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
  • 5
    It just works with references. You can use HEAD, tag names, branch names or plain hashes. – d12frosted Apr 04 '16 at 16:28
  • 6
    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
  • 41
    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
  • 4
    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
161

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

git log -1 --pretty=format:%h 

The meaning of %h, from man git log, is:

%h
abbreviated commit hash

To see other format options, see man git log and search for the section that begins with the phrase "Placeholders that expand to information extracted from the commit:".

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
113
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,433
  • 2
  • 13
  • 10
  • 3
    Also works with `git log --pretty=oneline`, which unlike `--oneline`, otherwise prints full size hashes. – sdaau May 16 '20 at 16:54
69

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,738
  • 21
  • 105
  • 131
The Gilbert Arenas Dagger
  • 12,071
  • 13
  • 66
  • 80
  • 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
41

A really simple way is to:

git describe --always
Steven Shaw
  • 6,063
  • 3
  • 33
  • 44
  • 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
  • 13
    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.
  • 15,659
  • 11
  • 54
  • 84
Fabrice
  • 307
  • 3
  • 6
18

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,738
  • 21
  • 105
  • 131
Down the Stream
  • 639
  • 7
  • 10
  • 1
    While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value. – SherylHohman Mar 07 '18 at 18:07
6

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,630
  • 21
  • 24
0

Also, if you need to get short commit SHA from the remote repository you can use the next command:

git ls-remote https://some.domain/some-remote-repo.git HEAD | awk '{ print substr($1,1,8) }'
PRIHLOP
  • 1,441
  • 1
  • 16
  • 16