How to Get Last Commit Hash in Git

Osvaldo Toja bio photo By Osvaldo Toja Comment

The bash way

$ git log -n1 | grep commit | awk '{print $2}'
fc77768a4f7c460be765012c9a04e9645e4520d2

The git way

# short - using h
$ git log --pretty=format:'%h' -n 1
fc77768
# long - using H
$ git log --pretty=format:'%H' -n 1
fc77768a4f7c460be765012c9a04e9645e4520d2

You can specify the number of digits of the hash using --abbrev=n

$ git show --pretty=%h --abbrev=18
fc77768a4f7c460be7
comments powered by Disqus