Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git-Repo searching for String in all committed files (unknown branch and commit)

Tags:

git

I have a piece of code in another than my current branch in my git repo, and I am not sure which commit and which branch. How can I search in all files that I committed until now for a specific string (and afterwards show the surrounding of that line of code)?

like image 246
Yo Ludke Avatar asked Dec 14 '12 10:12

Yo Ludke


People also ask

What command can find all commits that add or remove the string?

This is also possible using git log . You can perform a search for a string, for example, a variable or method, and git log will give you the commits, adding or deleting the string from the history.

How do I search for a specific commit in git log?

Looking up changes for a specific commit If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

How can I search git branches for a file or directory?

`git grep` command is used to search in the checkout branch and local files.


1 Answers

Use git grep to locate the commit:

git grep "string" $(git rev-list --all) 

The git rev-list --all makes it search the entire history of the project.

That will give an output like this:

<commit>:<path>:<matched line> 

Then you can use git branch --contains to find out which branch the commit is on:

git branch --contains <commit> 
like image 161
John Szakmeister Avatar answered Oct 09 '22 00:10

John Szakmeister