6

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.

Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37
Nils-o-mat
  • 1,132
  • 17
  • 31
  • 1
    worth mentioning https://stackoverflow.com/questions/277546/can-i-use-git-to-search-for-matching-filenames-in-a-repository – Kay V Feb 17 '20 at 19:34

4 Answers4

7

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.

Community
  • 1
  • 1
Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
3

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.

Community
  • 1
  • 1
Greg Prisament
  • 2,166
  • 1
  • 17
  • 18
  • That looks interesting. I give it a try. – Nils-o-mat Jun 09 '15 at 06:54
  • @Nils-o-mat If you have a large directory structure `git ls-files` might be faster than `find .` – Sascha Wolf Jun 09 '15 at 07:04
  • The main downside for me is, that I loose deleted files here. That makes it not a viable option. – Nils-o-mat Jun 09 '15 at 07:05
  • 1
    Edited to address deleted files. – Greg Prisament Jun 09 '15 at 07:31
  • 1
    Seems like you were a bit quicker than me. Also we seem to know the same parts of Stackoverflow ;-) – Nils-o-mat Jun 09 '15 at 07:45
  • 3
    Using `find -iregex 'regex'` is much better than `find | grep 'regex'` -- for one thing, with `-print0` you can feed even extremely weird matches to `xargs -0`. (For another, saving one process is always good practice, but that's obviously only of marginal importance here.) – tripleee Jun 09 '15 at 07:50
  • 1
    I think the last answer is best, but it will do some strange things on repositories where the regex matches a lot of files. Also it will choke if the files have spaces or special characters in their names. – Michael Anderson Jun 10 '15 at 00:03
0

Thanks to your answers (especially Greg and Michael) I developed a way myself. (I hope this proves viable):

git log --name-only --pretty="format:"|sort -u|egrep '<REGEX>'|xargs git log --
Nils-o-mat
  • 1,132
  • 17
  • 31
-1

Can you do something like:

git log | grep [string_to_look_for]
locoboy
  • 38,002
  • 70
  • 184
  • 260