0

I want to list all files changed in the recent X commits.

I tried:

git log --name-status -10

But it also logs additional information like commit id, author, date etc. I only need the filename. Is there a command to achieve this?

EDIT:

This edit should explain why my question is not a duplicate of "How to list only the file names that changed between two commits?" as the user phd claims.

I don't think that I have to explain it, it should be obvious that these are two entirely different questions. I asked how to create a list of the last x commits, while the "duplicate" asks about a list of commits between commit A and B.

Black
  • 18,150
  • 39
  • 158
  • 271
  • 1
    Possible duplicate of [How to list only the file names that changed between two commits?](https://stackoverflow.com/questions/1552340/how-to-list-only-the-file-names-that-changed-between-two-commits) – phd Sep 11 '19 at 11:23
  • https://stackoverflow.com/search?q=%5Bgit%5D+list+changed+files – phd Sep 11 '19 at 11:23
  • @phd, how is this a duplicate of "How to list only the file names that changed between two commits?" ??? Explain – Black Sep 11 '19 at 12:26
  • 1
    @Black I'd have to side with phd on this one. It's a very similar need. Name one relevant difference between your question and the one phd linked. There are nuances, yes, but the analysis and solution are nearly identical. – Romain Valeri Sep 11 '19 at 12:39
  • 1
    @Black You asked "all files changed in the recent X commits" then "I only need the filename". What did I miss? (and to be clear, I'm not calling out your question as bad in any way, that's why I answered, it's a *good* question.) – Romain Valeri Sep 11 '19 at 12:42

3 Answers3

2

I prefer using git diff here:

git diff --name-only HEAD~10.. --

On one of the tags in the Python repo, this produced:

$ git diff --name-only HEAD~10.. --
Doc/library/http.client.rst
Doc/library/tkinter.rst
Include/patchlevel.h
Lib/http/client.py
Lib/ssl.py
Lib/test/libregrtest/setup.py
Lib/test/test_httplib.py
Lib/test/test_ssl.py
Lib/test/test_syntax.py
Mac/BuildScript/resources/ReadMe.rtf
Mac/BuildScript/resources/Welcome.rtf
Misc/NEWS.d/3.7.4.rst
Misc/NEWS.d/3.7.4rc1.rst
Misc/NEWS.d/3.7.4rc2.rst
Misc/NEWS.d/next/Core and Builtins/2019-06-22-12-45-20.bpo-24214.hIiHeD.rst
Misc/NEWS.d/next/Library/2019-02-03-19-13-08.bpo-32627.b68f64.rst
Misc/NEWS.d/next/Library/2019-06-27-13-27-02.bpo-37428._wcwUd.rst
Misc/NEWS.d/next/Library/2019-06-27-20-33-50.bpo-37437.du39_A.rst
Misc/NEWS.d/next/Windows/2019-06-18-09-05-08.bpo-35360.tdqSmo.rst
Misc/NEWS.d/next/Windows/2019-06-28-08-09-08.bpo-37369.1iVpxq.rst
Modules/expat/expat_external.h
Python/compile.c
Python/peephole.c
README.rst
configure
configure.ac

If you want the status, you can use the --name-status option with diff too:

git diff --name-status HEAD~10.. --

Here the example above with the --name-status option:

$ git diff --name-only HEAD~10.. --
M   Doc/library/http.client.rst
M   Doc/library/tkinter.rst
M   Include/patchlevel.h
M   Lib/http/client.py
M   Lib/ssl.py
M   Lib/test/libregrtest/setup.py
M   Lib/test/test_httplib.py
M   Lib/test/test_ssl.py
M   Lib/test/test_syntax.py
M   Mac/BuildScript/resources/ReadMe.rtf
M   Mac/BuildScript/resources/Welcome.rtf
A   Misc/NEWS.d/3.7.4.rst
M   Misc/NEWS.d/3.7.4rc1.rst
A   Misc/NEWS.d/3.7.4rc2.rst
D   Misc/NEWS.d/next/Core and Builtins/2019-06-22-12-45-20.bpo-24214.hIiHeD.rst
D   Misc/NEWS.d/next/Library/2019-02-03-19-13-08.bpo-32627.b68f64.rst
D   Misc/NEWS.d/next/Library/2019-06-27-13-27-02.bpo-37428._wcwUd.rst
D   Misc/NEWS.d/next/Library/2019-06-27-20-33-50.bpo-37437.du39_A.rst
D   Misc/NEWS.d/next/Windows/2019-06-18-09-05-08.bpo-35360.tdqSmo.rst
D   Misc/NEWS.d/next/Windows/2019-06-28-08-09-08.bpo-37369.1iVpxq.rst
M   Modules/expat/expat_external.h
M   Python/compile.c
M   Python/peephole.c
M   README.rst
M   configure
M   configure.ac

Personally, I prefer using --stat to see some statistics about the changes, if it's meant to be consumed by me versus a script:

git diff --stat HEAD~10.. --

Here's the same example above with --stat:

$ git diff --stat HEAD~10.. --
 Doc/library/http.client.rst                        |  5 ++
 Doc/library/tkinter.rst                            |  4 +-
 Include/patchlevel.h                               |  6 +-
 Lib/http/client.py                                 |  7 ++
 Lib/ssl.py                                         | 29 ++++---
 Lib/test/libregrtest/setup.py                      | 16 ----
 Lib/test/test_httplib.py                           | 18 +++++
 Lib/test/test_ssl.py                               |  9 ++-
 Lib/test/test_syntax.py                            | 14 ----
 Mac/BuildScript/resources/ReadMe.rtf               |  8 +-
 Mac/BuildScript/resources/Welcome.rtf              |  4 +-
 Misc/NEWS.d/3.7.4.rst                              | 19 +++++
 Misc/NEWS.d/3.7.4rc1.rst                           |  2 +-
 Misc/NEWS.d/3.7.4rc2.rst                           | 90 ++++++++++++++++++++++
 .../2019-06-22-12-45-20.bpo-24214.hIiHeD.rst       |  2 -
 .../2019-02-03-19-13-08.bpo-32627.b68f64.rst       |  1 -
 .../2019-06-27-13-27-02.bpo-37428._wcwUd.rst       |  4 -
 .../2019-06-27-20-33-50.bpo-37437.du39_A.rst       |  1 -
 .../2019-06-18-09-05-08.bpo-35360.tdqSmo.rst       |  1 -
 .../2019-06-28-08-09-08.bpo-37369.1iVpxq.rst       |  1 -
 Modules/expat/expat_external.h                     |  4 +
 Python/compile.c                                   |  9 ++-
 Python/peephole.c                                  | 15 +---
 README.rst                                         |  4 +-
 configure                                          |  6 ++
 configure.ac                                       |  6 ++
 26 files changed, 209 insertions(+), 76 deletions(-)

(note: the output will adjust width based on the terminal in this last version)

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
  • Yes, I guess it depends on the need. To feed it to a script, or constitute a filepack, better have only the list itself, but for human consumption, I'm with you here. – Romain Valeri Sep 11 '19 at 08:07
  • However.... OP asked : "I only need the filename" so why file statuses? why the diff? why the stats? – Romain Valeri Sep 11 '19 at 12:37
  • @RomainValeri Because the OP also said he "solved it" by using `git log --name-status --oneline -10`. So given that answer, it seemed like the OP was actually after something else. Either way, `git diff` is nice here because you get a list of files touched without having to pipe the result through another command to get a list of unique filenames. I think `git log` is a less optimal answer, IMHO. – John Szakmeister Sep 12 '19 at 00:59
  • I don't know why yet, but I compared the outputs of `git log --name-only --pretty=format:"" origin/master..origin/releases` and `git diff --name-only origin/master..origin/releases` and on a work repo I have here, and it yields slightly different results... I have to figure why. – Romain Valeri Sep 12 '19 at 10:06
  • I wonder if that’s git log’s history simplification at work? It could be that things like commits being reverted don’t show up using git diff since those changes are no longer present, whereas will show you everything that was touched even if the net result is no change for a particular file. – John Szakmeister Sep 12 '19 at 11:09
1

The classic way would be to

git log --pretty=format:"" --name-only -10 | sort -u

| sort -u helps with sorting and getting rid of doubles, while --name-only outputs the file list without status letters.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • How is this better than using `git diff`? – John Szakmeister Sep 12 '19 at 00:55
  • @JohnSzakmeister It's *not* better, your diff proposal is better for a number of things, I tend to use it myself often. But if I got the question right, this solution is closer to what OP wanted because he just wants a filenames list. No? – Romain Valeri Sep 12 '19 at 06:48
  • `git diff --name-only` will do exactly that and you get to skip the piping through `sort -u` as it will not contain any duplicates in there. :-) It will not contain the actual changes, only the files touched between the compared revisions. – John Szakmeister Sep 12 '19 at 08:04
  • @JohnSzakmeister I agree, `git diff --name-only` does the job indeed, my remarks were only adressing the statuses and stats part. And I get how OP's self-answer can have influenced the way to look at his question, I think I overlooked that. – Romain Valeri Sep 12 '19 at 08:17
  • No worries. I was more interested in making sure you knew that `git diff --name-only` actually does the job and doesn't need any external commands to get it done. :-) – John Szakmeister Sep 12 '19 at 08:22
  • @JohnSzakmeister To be honest, when I first read your answer I was a bit puzzled but tried it and indeed it allows to skip the sorting part, it's great :-) – Romain Valeri Sep 12 '19 at 08:26
-1

I solved it... by adding --oneline.

git log --name-status --oneline -10
Black
  • 18,150
  • 39
  • 158
  • 271