Michal Vašut Michal Vašut - 1 year ago 84
Git Question

How can I get what is the revision of file in Git

Is there any way to get revision (ie commit hash) of the file in Git?

I know about

git log -- <file_path>
), but that gives me all commits (or the last one if I do
git log -1 -- <file_path>
), but that's not what I want. Lets have FILE with commits A,B,C,D,E where E is HEAD now I'll do something like this

git checkout C -- FILE

Now if I do
git status
, it shows me that the FILE is modified and needs to be committed - what I don't want to do. (I have simple history viewer in my app and need to mark revision of the file after the checkout).

Is there any way to make Git show me current revision (in this case commit C)?

SVN can do this by
svn info <file_name>
, but SVN is not Git and works differently.


in SVN:

svn info FILE // gives me revision E
svn update -r C
svn info File // gives me revision C

EDIT2: Real world example:

$ git log --pretty=format:"%H ... %h ... %s" -- MySQL\ 5_5.txp
131b81c2a203c564c01b4fffa98b7395ebb75bc2 ... 131b81c ... modified MYSql5.5
2640738310efa21f119cdcb252cc5069aade27bd ... 2640738 ... Changed Entity 2 position
bbe9b2474b3f0127da0271b4db74712c201c2c03 ... bbe9b24 ... Changed Entity 3 position
c40c5f74599515d95b17641a58c2864a34b20f06 ... c40c5f7 ... from popup
ac46fa7d4d93e91183a033ce8fffcf93d9f0505d ... ac46fa7 ... from toolbar
248824ec7f4fda13ca28358d7b50a810b2d247f9 ... 248824e ... sdsada
20103e2c1798cee2816eb78967cdb6443bd1f9bf ... 20103e2 ... mysql
e222f54962b9b12745b0c1904b70f1ee9c657d2e ... e222f54 ... ssss
fd8f3b34a86851f54cf2951cb4d3077e4ffa16ba ... fd8f3b3 ... ssss
c979f4cba91564333fa81e62dce328c23c38b739 ... c979f4c ... ccccccccccc
9a5a8e08c016a27b7d818c8e6828dd5dd578a00c ... 9a5a8e0 ... divny commit
61c8146060d1db337888d7bca83db1bacb441158 ... 61c8146 ... edwdewd
d495e292bb917fe730143af5b40c612a6937eaea ... d495e29 ... wqewqe
459d7938df6273bc4b8a4f3aa6a41e0d318a291d ... 459d793 ... wqwewqe
6f993b23ec79350c57f75375e3640d5079d8b873 ... 6f993b2 ... aaaaaaaa
7c9e43cda6ce44d4e614a42637b3334de4813059 ... 7c9e43c ... ddwedwe
ea419227ad22058625aadd08aa7e9227063568ee ... ea41922 ... edwd
2c08393f0696d443ab5ad8b75d74093d59caf76f ... 2c08393 ... mysql

$ git checkout e222f54 MySQL\ 5_5.txp

$ git rev-parse HEAD:MySQL\ 5_5.txp

I would expect something like:

$ git <some command> [options] MySQL\ 5_5.txp

to return
or fulhash

Answer Source

This shell script should work, just pass file name as an argument:


blob=`git hash-object $1`

git log --format=format:%H --follow $1 |
while read commit; do
    if git ls-tree -r $commit | grep -q $blob; then
        echo $commit; break;
  1. git hash-object $1 calculates hash for currently checked out file $1
  2. git log --format=format:%H --follow $1 lists hashes for all commits which introduced any change to file $1
  3. git ls-tree -r $commit | grep -q $blob; finds which commit from step 2 has file $1 with the same hash as calculated in step 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download