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.
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
[update]
additionally: if you can't find a blob matching the exact hash of your file, you can resort to one of the pickaxe options -G
or -S
:
git log -G "<text>" --all
(resp: git log -S "<text>" --all
) to find commits which contain (resp: change the count of occurences) this text in their diffYou can add --name-status
(or --name-only
) to have git log
list the files that contain such a diff.
This should narrow down your search.