Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find deleted content in a Git repository; I only know surrounding context

Tags:

git

I want to get back some code that I deleted in some commit some time ago. I don't remember what the code was at all, but I remembered that it did something very useful, but I deleted it because I thought I wouldn't need it. However, I now need that code back, but I only remember what function it was in.

Other information: the file containing that function also contains 500 lines of code total. There is a 30 commit range I know that the code appeared in at one point.

This question is a high level problem. How can I use the information I know to get the information I want?

like image 623
Alexander Bird Avatar asked Oct 06 '22 09:10

Alexander Bird


1 Answers

  1. The best you can do is with search keywords. If you know a function name

    git log -Sfunction
    

    would be perfect.

  2. If you have a pattern you can look for in each commit's changes:

    git log --grep=pattern 
    

    Will list commits containing that pattern. Add -i for case insensitive match. Add --regex for regular expression match

  3. Otherwise

    git log -p FIRST...LAST | less
    

    will give you full text. You could search, or just scroll and visually scan...


Oh. PS. Since you mention it is a long function, you could just do

    git log --stat FIRST...LAST

And watch for files with many deletions (----) in the diff stats.

like image 160
sehe Avatar answered Oct 10 '22 02:10

sehe