5

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alexander Bird
  • 38,679
  • 42
  • 124
  • 159

3 Answers3

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

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    `git log -S` worked great for me in a similar situation. I needed to see the code that used to be in a Ruby class that was deleted a while back, and this took me right to it. Awesome! :) – Steve Jorgensen Oct 22 '13 at 21:28
1

Use git blame with

--reverse

Walk the history forward instead of backward. Instead of showing the revision in which a line appeared, this shows the last revision in which a line has existed. This requires a range of revisions, like START..END where the path to blame exists in START.

Or try to play with git bisect to do binary search in your 30 commits to find the one where the code of interest appears.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eugene Sajine
  • 8,104
  • 3
  • 23
  • 28
0

If you can remember the function name, or any other significant word or variable, then you can try git grep

git grep <regexp> $(git rev-list <rev1>..<rev2>)

Where rev1 and rev2 are the limits of your commit range. It will grep all the commit content (including deletion), so you will certainly find your code.

For more details see How to grep (search) committed code in the git history?

Community
  • 1
  • 1
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • From what I understand, this will output all lines of code containing the function name? If that's so, I don't want those lines, I want ones _near_ it. However, the deleted code is not near the beginning of the function name for the function name to appear in the diffs. Also, the function itself is in every commit. So unfortunately this doesn't help me. -- unless I am misunderstanding something – Alexander Bird Oct 10 '12 at 21:49
  • 1
    so you have the function name, it still exists, and you're looking for code near to it, right? then you know what file to lookfile at; sounds like `git blame` will do the job – CharlesB Oct 11 '12 at 05:23