Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find where code was deleted in a Git repository

Tags:

git

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?

like image 949
Ivan Avatar asked Oct 06 '09 22:10

Ivan


People also ask

Can you see deleted files GitHub?

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.

Does git diff show deleted files?

You can use git diff --name-status, that will show you files that where added, modified and deleted.


2 Answers

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?

like image 164
Pat Notz Avatar answered Oct 17 '22 22:10

Pat Notz


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>.

like image 31
Simone Avatar answered Oct 17 '22 23:10

Simone