1

I have a file taken from a repository some time in the distant past. Is there a way to tell what commit this file is related to?


I am well aware of the commit history. That's not what I am asking. I am asking to find what was the commit associated with that specific file version. I don't know what changed with respect to the previous commit, nor what changed in the following commit, so a simple history does not do the trick.

A brute-force check would be to systematically check out every commit and compare the file in the repository to the outdated copy I have, until I find the matching commit.

ysap
  • 7,723
  • 7
  • 59
  • 122
  • 1
    `git log -- path/to/file` wll give you the history of commits touching (add, modify, create, whatever) that path. I can't believe there's not a dozen duplicates of this already. I'll search for one later if you guys didn't already. – Romain Valeri Aug 24 '22 at 12:40
  • I may have not been clear enough - I am well aware of commit history. That's not what I am asking. I am asking to find what was the commit associated with that file version. I don't know what changed wrt the previous commit, nor what changed the following commit, so a simple history does not do the trick. – ysap Aug 24 '22 at 12:54
  • 1
    It's indeed a different question. Clearer now, thank you. Reopened. – Romain Valeri Aug 24 '22 at 13:04
  • Start at https://stackoverflow.com/a/49059971/7976758 No need to checkout every commit, it'd be slow; just do `git diff "$rev:path/to/file"` – phd Aug 24 '22 at 13:10

1 Answers1

6

git log has a --find-object=<hash> option.

You can compute the hash for that exact version of the file, and ask Git what commits added or removed a file with that specific hash:

hash=$(git hash-object that/file)

# Note: you can run 'git hash-object' and 'git log --find-object' 
#       on two different machines
git log --oneline --find-object=$hash --all
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • 1
    I wondered about using `hash-object` to know the blob hash, but failed to think about `--find-object`... Well done! – Romain Valeri Aug 24 '22 at 13:41
  • I missed this detail at first, but this command will show both the commit that added the given object, and the commit that removed that particular object. So if you're doing this on an old version of the file, you would expect to get (at least) two commits printed. – joanis Aug 24 '22 at 13:58
  • 1
    And I just did the test, with a file that got renamed, it gave me three commits: the one that introduced that version, the one that renamed it, and the one that modified the renamed file. Very useful answer, thank you! – joanis Aug 24 '22 at 14:00
  • This is just great! When your repo is 8 years old, and the file was 5 years old, this is a saver. – ysap Aug 24 '22 at 14:02
  • @joanis: thanks for testing ! with the "works like `-S`" in the documentation, I would have thought that a commit with a "rename" action would be ignored. – LeGEC Aug 24 '22 at 14:06
  • @LeGEC Yes, reading the manual, that's exactly what I expected. To me, "Look for differences that change the number of occurrences of the specified object." should have meant the rename would not be listed, so I was surprised when the rename did in fact get listed. – joanis Aug 24 '22 at 14:07