Reputation: 40678
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?
Upvotes: 5
Views: 1262
Reputation: 8200
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.
Upvotes: 1
Reputation: 393769
The best you can do is with search keywords. If you know a function name
git log -Sfunction
would be perfect.
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
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.
Upvotes: 3
Reputation: 90496
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?
Upvotes: 0