1

I have a file, lets say, test.txt. Lets say that its empty file and I committed it, so my first commit would just be an empty file. lets say sha for that is 2d37f7

Now Lets say I edited the file and added some text "hello world" in the file and I committed that file. Now I have another hash with hello world version and lets say that that hash is eec1598

Now when I do git log, I see this

eec1598 (my last commit on top)
2d37f7 (my first empty file)

Now lets say that I want to go back to the first file, I do something like this

git checkout 2d37f7 test.txt

when now I check the file, its empty , which is good. But when I do

git log test.txt

it shows me the same thing as above

eec1598 (my last commit on top)
2d37f7 (my first empty file)

At this point I don't know which version of test.txt is current version. How do I find that out?

thanks

Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221
  • Possible duplicate of [Which commit has this blob?](http://stackoverflow.com/questions/223678/which-commit-has-this-blob) – Jonas Malaco Feb 05 '16 at 20:29

1 Answers1

2

Note that the shas that are shown in git log are for the commits, not the file. So what you are asking is how to determine which commit the current working copy of test.txt came from. Unfortunately, that information is not stored anywhere. In most cases, there is no single commit that you can say the file came from, as the file will have the same contents in multiple commits.

So in summary, after a git checkout <sha> <filename>, git just considers your working copy of filename to be like any other file which you have locally changed; it is not tied to any particular commit.

Now, this being said, you can get the hash of your current version of test.txt with git hash-object, then pass that hash to a script that lists all commits that contain that version of the file. To see a couple examples of this kind of script see the first answer to this question.

Community
  • 1
  • 1
David Deutsch
  • 17,443
  • 4
  • 47
  • 54