8

I have a git diff output that contains lines like this: index 0056c73..92c6cbd 100644 for each file. I know which repository it comes from, but unfortunately have no idea which revision of the repository it is diffing against.

How do I find the commit of the repository that the diff is against?

Alternately, how do I find the exact commit of each file that the pre-image hashes in the diff correspond to? (i.e. which version of a file has the 0056c73 hash in the example above)

Alex I
  • 19,689
  • 9
  • 86
  • 158

2 Answers2

3

If you know the path of the file, you can start displaying all the commits for said path:

git log --all --pretty=format:%H <path>

If 0056c73 is a blob SHA1 for that file, a git ls-tree will print all entries SHA1, and you can grep the one you are after.

"Which commit has this blob?" proposes this one-liner from aragaer:

git log --all --pretty=format:%H <path> | xargs -n1 -I% sh -c "git ls-tree % <path> | grep -q <hash> && echo %"

(replace <hash> with 0056c73)

As I mentioned in "Which commit has this blob?", with Git 2.16+ (Dec. 2017), you can use:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Note this looks by blob hash as returned by `git hash-object` or `sha1("blob " + filesize + "\0" + content)`, and not regular sha1sum of the file content. That's indeed the hashes uses in `index 0056c73..92c6cbd` diif headers. It just surprised me as I had actual file contents and wanted to find commit... – Beni Cherniavsky-Paskin Oct 09 '18 at 10:06
  • `git log --find-object=` worked for me. Thanks. – Andriy Makukha Oct 24 '18 at 07:54
1

As elaborated in https://stackoverflow.com/a/48027778/239657, since Git 2.16 (Q1 2018), you can simply run git describe 0056c73. Example on another repo:

$ git show d69fe27cf937398fa2bf6813674a3975bfe56e89 | grep e1cfe5
index 6e180f8221..e1cfe56bf6 100755
$ git describe e1cfe56bf6
1.1.0.Beta2-7645-gd69fe27cf9:adapters/oidc/js/src/main/resources/keycloak.js
$ git describe e1cfe56bf6 --abbrev=40
1.1.0.Beta2-7645-gd69fe27cf937398fa2bf6813674a3975bfe56e89:adapters/oidc/js/src/main/resources/keycloak.js

You can play with git describe flags to tweak the friendly lookup of close tags vs just showing the commit hash...

Beni Cherniavsky-Paskin
  • 9,483
  • 2
  • 50
  • 58
  • Good point. I have edited my answer to reflect that, since the link you mention... is an answer I wrote. – VonC Oct 09 '18 at 10:55