Drathier
Drathier

Reputation: 14539

Is this the (previous) contents of a file in this git repo?

I have a file I found laying around. I want to know if it came from a specific git repo, at some point in time. Exact-match comparison is good enough. How can I do this?

Upvotes: 3

Views: 56

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382274

You can generate the SHA from a file using

git hash_object <file path>

This gives you a hash such as this one:

c675fb0fe881673391f078c37e594ec7a51aa222

It's also possible to list all (reachable) blobs and filenames using a command like this one (many variations possible).

Using this, you can grep your hash:

git rev-list --objects --all | git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' | grep '^[^ ]* blob' | cut -d" " -f1,3- | grep c675fb0fe881673391f078c37e594ec7a51aa222

Upvotes: 3

Related Questions