1
$ git init
$ echo zero > a
$ git add a
$ git commit -m zero
$ echo other > a
$ git add a
$ git commit -m other
$ echo whatever > a
$ git add a
$ git commit -m whatever

I'm checking out the file as it was when I commited the 2nd time, when the file contained the text other.

$ git checkout HASH_OF_COMMIT_OTHER a
$ cat a
other
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   a
#

What command should I run on a, so that git will tell at which changeset it currently is? In this case I would like it to show me the hash of the 2nd commit, along with the commit message, if possible.

Geo
  • 93,257
  • 117
  • 344
  • 520
  • 1
    What Mark says is correct, but I'd be surprised if that helps you. It would be best if you told us why you wish to do this. – Gerry Aug 18 '11 at 17:58
  • what happens when you revert to an old version of `a` and re-commit? Which changeset "should" it report to you - the newer or the older? That's just _one_ of the things that makes this difficult... – johnny Aug 18 '11 at 18:04
  • Sometimes, when I do some tests, I need to compare results between various changesets. So, I checkout older versions, produce some output and compare them. The thing is, sometimes I forgot what revisions I checked out :D – Geo Aug 18 '11 at 18:19

2 Answers2

3

Once you've run the command git checkout HASH_OF_COMMIT_OTHER a, the working tree and the index are updated with the version of a from HASH_OF_COMMIT, but there's no record preserved of how the file was changed to be in that state. This is part of git's philosophy of tracking content - it doesn't matter how the file got into the state it is at a certain commit, it just records the exact content of the files at each one.

You could certainly write a short script to find all commits that contained a file with exactly that content (and at that path) if you like, but normally one wouldn't need to.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
1

After a great deal of searching for ways to do it using standard git commands I was lead back to this SO question which has just what you are looking for.

Which commit has this blob?

Community
  • 1
  • 1
Gerry
  • 10,584
  • 4
  • 41
  • 49