Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the hash of a file in Git history

Tags:

git

hash

I try to find the hash of a file at a given point in the history.

git ls-files $REVISION $PATH | \
  sed -n 's/^.\{6\} .\+ \([0-9a-f]\{40\}\)\t.*$/\1/p'

works, but it looks rather inelegant. It displays mode, type, hash and path to then use sed to filter out only the hash. Is there some command that only prints the hash to begin with?

I imagine to have overlooked something more straightforward like git show --pretty=%hash $REVISION:$PATH. Does a Git command like this exist?

like image 740
XZS Avatar asked Oct 22 '15 15:10

XZS


1 Answers

Git provides the rev-parse command for printing the SHAs of various items. Unlike many Git commands (pretty much everything but git show) it uses a colon as a separator between revisions and paths.

git rev-parse $REVISION:$PATH 
like image 50
Andrew C Avatar answered Oct 31 '22 10:10

Andrew C