Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search filenames with regex

Tags:

git

regex

Is there any way to do something like git log <path>, but instead of path using a regex? I want to search commits containing files, whose filenames match a given pattern...

... and while we're at it: Is there also a way to do a git status / git diff only for filenames matching a given pattern?

Thanks in advance!

EDIT: I would be terrific if any way to do it, would also work for Git v1.7.1.

like image 783
Nils-o-mat Avatar asked Jun 09 '15 06:06

Nils-o-mat


People also ask

How do I search for a file in RegEx?

In order to search files using a regular expression, select the 'File Name' file matching rule, select the 'RegEx' pattern matching operator and enter a regular expression that should be matched. For example, the '\. (JPG|BMP|PNG)$' regular expression will match all JPG, BMP and PNG image files.

Can I use RegEx with find?

To use regular expressions, open either the Find pane or the Replace pane and select the Use check box. When you next click Find Next, the search string is evaluated as a regular expression. When a regular expression contains characters, it usually means that the text being searched must match those characters.

What does this RegEx do?

Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text. Perl is a great example of a programming language that utilizes regular expressions. However, its only one of the many places you can find regular expressions.


2 Answers

As far as a pure git solution goes and I'm aware of the only option to match specific file patterns is to use a glob.

git log -- '*.json'

Will give you all files which contain changes to a json file. The same can be done for git status.


On the other hand it's quite easy to search for regular expressions in the diff or the commit message. git log offers a --grep option to search for matches in the commit message and a -S option to search for strings.

Take a look at this question for further details.

like image 158
Sascha Wolf Avatar answered Sep 30 '22 16:09

Sascha Wolf


For a simple pattern you could try, for example:

find . -name "*.c" | xargs git log

For a full-blown regex you can use:

find . | grep "REGEX" | xargs git log

If you need previously deleted files to be included in the output, you can use

git log --all --pretty=format: --name-only --diff-filter=A | sort -u | grep "REGEX" | xargs git log --

The first part of the above command, which finds all files that were ever in git, was lifted from an answser to this other question.

like image 29
Greg Prisament Avatar answered Sep 30 '22 15:09

Greg Prisament