Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git history - find lost line by keyword

I had somewhere in my Git repository a line containing the word "Foo" a couple of hundreds of commits before.

If there is any way to find its revision number where it was the last time?

like image 300
Nikita Fedyashev Avatar asked May 15 '10 07:05

Nikita Fedyashev


People also ask

How do I search text history in git?

Line Log Search Simply run git log with the -L option, and it will show you the history of a function or line of code in your codebase.

How do I search for a string in git log?

Git Browsing the history Searching commit string in git log Will search for removed file string in all logs in all branches. Starting from git 2.4+, the search can be inverted using the --invert-grep option. Will show all commits that do not contain add file .

How do I see my full git history?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.


1 Answers

That may be addressed by the pickaxe (-S) option of gitlog

 git log -SFoo -- path_containing_change 

(you can even add a time range: --since=2009.1.1 --until=2010.1.1)

-S<string> 

Look for differences that introduce or remove an instance of <string>.
Note that this is different than the string simply appearing in diff output; see the pickaxe entry in gitdiffcore(7) for more details.

diffcore-pickaxe 

This transformation is used to find filepairs that represent changes that touch a specified string.
When diffcore-pickaxe is in use, it checks if there are filepairs whose "original" side has the specified string and whose "result" side does not.
Such a filepair represents "the string appeared in this changeset".
It also checks for the opposite case that loses the specified string.


Update 2014:

Since then, you can do (from nilbus's answer):

git log -p --all -S 'search string' git log -p --all -G 'match regular expression' 

These log commands list commits that add or remove the given search string/regex, (generally) more recent first.
The -p (--patch) option causes the relevant diff to be shown where the pattern was added or removed, so you can see it in context.

Having found a relevant commit that adds the text you were looking for (eg. 8beeff00d), find the branches that contain the commit:

git branch -a --contains 8beeff00d 

(I reference that last command in "How to list branches that contain a given commit?")

like image 83
VonC Avatar answered Oct 14 '22 12:10

VonC