Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep files in git repo?

Tags:

git

grep

I love git grep to search in all files checked in to a repo. It's great. But is it possible to use it (or some other git command) to just use to find files (independent of content)?

At the moment I do this:

$ find . | grep middleware

which works but it's not using the git index which means it's going through every found file and it reports on files that are matching the .gitignore.

Any ideas for clever tricks?

like image 755
Peter Bengtsson Avatar asked Mar 09 '11 11:03

Peter Bengtsson


People also ask

How do I find a file in a git repository?

Usage. While browsing your Git repository, start typing in the path control box to search for the file or folder you are looking for. The interface lists the results starting from your current folder followed by matching items from across the repo.

What files can be searched using git grep?

Git ships with a command called grep that allows you to easily search through any committed tree, the working directory, or even the index for a string or regular expression. For the examples that follow, we'll search through the source code for Git itself.

How do I search inside files on GitHub?

You can search for a file in a repository using the file finder. To search for a file in multiple repositories on GitHub, use the filename code search qualifier.

How do I search git bash?

To be sure that you are using the Unix find from the Git Bash shell, type /bin/find . Now the -name parameter will work as shown in other answers. I'm using git for Windows and find .


2 Answers

Maybe you want git ls-files which lists the files in the index? (and automatically adjusts for your current directory inside the git work directory)

like image 111
araqnid Avatar answered Oct 19 '22 22:10

araqnid


I think git ls-files will do the trick for you.

So:

 git ls-files "*middleware*"
like image 27
Steven Avatar answered Oct 19 '22 22:10

Steven