-1

I've been able to print the all the logs that contain a particular string in their commit messages using:

git log --grep=<string>

Is it possible to get a list of all the files affected in a similar way. viz. all the files that were affected by the commits that the previous command shows.

Based on Amber's post, a clearer output I was envisaging was something like this:

> git log -p --name-only --grep=<string>
a/b/c/d/e/f/a.java
a/b/c/d/e/f/b.java
a/b/c/d/e/c.java
a/b/c/f.java
a/b/c/d/e/f/s/t.java

As an aside, if there is a good place to start on learning git commands, please point me to it. Thanks.

Explanation on why this question is not a duplicate of other questions already asked: I don't want the commit content, just he files that were changed without duplicating the names of the files (if they were used in multiple commits).

Rajath
  • 11,787
  • 7
  • 48
  • 62
  • Duplicate of https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history and friends – Dima Tisnek Jan 24 '19 at 01:19
  • Possible duplicate of [How to grep (search) committed code in the git history?](https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history) – Dima Tisnek Jan 24 '19 at 01:19
  • Possible, but it's not. – Rajath Jan 24 '19 at 02:39

1 Answers1

3

Try adding -p --name-status to your git log command.

  • -p tells git log to include a patch (diff) for each commit listed.
  • --name-status tells the diff generation to only include the name and status (new, changed, renamed, removed, etc) for each file in the diff, rather than the full diff of changes for each file.

So that'd be, in your case...

git log -p --name-status --grep=<string>

If you don't care about how each file was affected, but just want the list of files touched, you could use --name-only instead of --name-status.

Interested in knowing more options for git log? Take a look at the git-log manpage, which should also be available via git help log in your terminal.

Nothing but a list of files

We can add a few more options to elide all of the commit information and then remove duplicate entries:

git log -p --name-only --format="" --grep=<string> | sort | uniq

This should result in output that is nothing but a unique list of files that were touched by at least one commit matching the grep string.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • That's great, Amber. Simplified my search a lot, but I wanted to go a bit further. Is it possible to just print one list of files instead of grouping it under each commit? I've added to my question to make it clearer. – Rajath Jan 22 '19 at 00:38
  • Hm. If you're talking about a set of discontinuous commits, probably not - `git log` is generally about listing individual commits, not combining them. If the set of commits is continuous, you could use `git diff` to get the total diff from the first commit to the last` (with `--name-only` again), but otherwise, you'd probably need to get the individual lists and then combine them. What you're asking for is niche enough it probably isn't built-in functionality. – Amber Jan 22 '19 at 00:48
  • If you don't care about files being listed multiple times, you might be able to do things with format strings (`--format`, iirc) to get rid of the other details about the commits, and only get lists of files - which if you wanted unique paths, you might be able to get by piping the output to `| sort | uniq`. – Amber Jan 22 '19 at 00:50
  • You might try `git log -p --name-only --format="" --grep= | sort | uniq` and see if it works. I'm in an airport at the moment on a Chromebook so I can't easily test it myself. :) – Amber Jan 22 '19 at 00:51
  • Managed to test it via Google Cloud's handy web-based console shell. It seems to work, I've added it to the answer. – Amber Jan 22 '19 at 00:54
  • Beautiful! Thanks a bunch for your trouble. That's exactly what I was looking for. And much for me to learn in that one line. – Rajath Jan 22 '19 at 01:06