Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get abbrev-commit for a git branch, cleanly

Tags:

git

I need to extract abbreviated commit names for specific git branches. These are typically 7-digit hexadecimal numbers, and they're used by some systems (including Heroku's COMMIT_HASH environment variable) to identify a specific commit.

There's several ugly ways to get an abbreviated commit name, including:

$ git log -1 --oneline | awk '{ print $1 }'
d4377e3
$ git describe --always --match ''
d4377e3

Is there a cleaner way to get this value?

like image 204
emk Avatar asked Dec 21 '22 18:12

emk


1 Answers

Great question. I believe are looking for git-rev-parse, one of the low level git commands.

[jason@star Data]$ git-rev-parse --short github/master
8b81a38

Also, if you want the latest commit on current branch, just pass HEAD as the parameter.

[jason@star Data]$ git-rev-parse --short HEAD
8b81a38
like image 115
gahooa Avatar answered Jan 01 '23 17:01

gahooa