1

I have a version of a program laying around, let's say "mystery.c". I use git for this program's development.
I don't know which (if any) commit contains this specific version. For instance, I may have generated this version by copying at some intermediate stage between commits for some reason.

How do I ask git to find the most recent commit containing this file's contents exactly?
Or to say that the file's exact content is not in the history?
Or, more generally, to find the most similar recent match in the history?

SO Stinks
  • 3,258
  • 4
  • 32
  • 37

1 Answers1

1

You can simply use git log:

git log --follow filename

The --follow option is for listing the history of a file beyond renames (works only for a single file).

Compact version:

git log --follow --name-only --format='%H' -- filename

When it comes to find a file content, "Finding Content in Files With Git" is a good read, using combination of git log -S (pickaxe option) and git grep.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Note: this differs from finding all commits which includes a file: http://stackoverflow.com/a/6258472/6309 – VonC Dec 27 '14 at 12:50
  • That shows me a listing of the file's history but I want to find the file in the history (if any) that matches the content of a file outside the repository. I could probably whip some scripts up that checks the SHA1 sum of my file to see if it's in the history but I imagine there's a pre-made solution. – SO Stinks Dec 27 '14 at 13:35
  • @Dr.PersonPersonII if you have a specific string in your code, you could limit the number of commits to check with `git log -S`: http://stackoverflow.com/a/2928721/6309 – VonC Dec 27 '14 at 13:48