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 am I doing wrong?
If you have committed the deletion and pushed it to GitHub, it is possible to recover a deleted file using the GitHub Web UI. GitHub lets you browse the commit history and explore the project at any point in history, which then allows you to view and download any file.
You can use git diff --name-status, that will show you files that where added, modified and deleted.
Hmph, this 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?
git log -S<string>
does the job, but if you need to make more complex searches you can use git log -G<regex>
.
From the man
:
-G<regex>
Look for differences whose patch text contains added/removed lines that match
<regex>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With