MrLapDong
MrLapDong

Reputation: 19

How to get the commit hash ID for a file?

I used git checkout commit_hash path/to/file to rollback a file to an older version.

After that, I forgot the commit hash ID that I used to retrieve this file. I used git status path/to/file, it shows this file is modified even though I did nothing to this file (I think it compares to HEAD).

Is there any way to get commit hash ID of this retrieved file?

Upvotes: 1

Views: 1224

Answers (2)

ElpieKay
ElpieKay

Reputation: 30868

Try the command history | grep path/to/file.

Since there might be many commits that hold the same version of path/to/file, the result would not be precise enough through git commands. Here's one of the tries.

git hash-object path/to/file

And you'll get a hash number($hash). It's the blob name of the very version of path/to/file.

for commit in $(git rev-list --all --reflog);do
    git ls-tree -r $commit -- path/to/file | if grep $hash;then echo $commit;fi
done

All the candidates will be printed and you'll have to identify the commit in your memory among them all. Maybe then git log -1 $commit would make it more recognisable than then echo $commit.

Upvotes: 1

gamalan
gamalan

Reputation: 121

You could use

git log filename

It would show the file log starting from the latest. If you forget, you could use shell history command. Assuming it's not too old.

Upvotes: 1

Related Questions