tags:

views:

93

answers:

1

This is making me nuts.

How do I find code that was deleted?

I ended up finding where it was created with this:

$ git log --pretty=oneline -S'some code'

And that's good enough, but I was also curious to find where it got deleted, and so far, no dice.

First, I tried git diff HEAD..HEAD^|grep 'some code', expanding the range each time, until I found the lines where it was removed. Nice, so suppose I found it on range HEAD^^..HEAD^^^, then I do git show HEAD^^^ and git show HEAD^^ with grep, but the code is nowhere to be found!

Then I read up a bit on git bisect, and sure enough, it gives me a single revision where the culprit is supposed to be... Again, git show rev|grep 'some code' comes up empty...

What the? What am I doing wrong?

Thanks!

A: 

Hmph, works for me:

$ git init
Initialized empty Git repository in /Users/pknotz/foo/.git/

$ echo "Hello" > a

$ git add a

$ git commit -am "initial commit"
[master (root-commit) 7e52a51] initial commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 a

$ echo " World" >> a

$ git commit -am "Be more specific"
[master 080e9fe] Be more specific
 1 files changed, 1 insertions(+), 0 deletions(-)

$ echo "Hello" > a

$ git commit -am "Be less specific"
[master 00f3fd0] Be less specific
 1 files changed, 0 insertions(+), 1 deletions(-)

$ cat a
Hello

$ git log -SWorld
commit 00f3fd0134d0d54aafbb9d959666efc5fd492b4f
Author: Pat Notz <[email protected]>
Date:   Tue Oct 6 17:20:48 2009 -0600

    Be less specific

commit 080e9fe84ff89aab9d9d51fb5d8d59e8f663ee7f
Author: Pat Notz <[email protected]>
Date:   Tue Oct 6 17:20:33 2009 -0600

    Be more specific

Or, is this not what you mean?

Pat Notz
That's what I mean... So if it doesn't work for me, could it be that the index is corrupted or history was rewritten?
Ivan
In this example, Pat is using `git log -SWorld` which does *not* show the diffs. I'm guessing (haven't tried it) that if the last command were `git show 00f3fd0134d0d54aafbb9d959666efc5fd492b4f | grep World` then you'd get the behavior you're looking for.
Simeon Fitch