Categories
git git-diff git-show

How to list only the names of files that changed between two commits

2447

I have a bunch of commits in the repository. I want to see a list of files changed between two commits – from SHA1 to SHA2.

What command should I use?

2

3284

git diff --name-only SHA1 SHA2

where you only need to include enough of the SHA hash to identify the commits. You can also do, for example

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

to see the differences between the tenth latest commit and the fifth latest (or so).

13

  • 182

    This works for git show as well. git show --name-only SHA1.

    Nov 23, 2009 at 13:11

  • 102

    git diff --name-status [TAG|SHA1] shows what operations were done to the files too

    – reconbot

    Sep 28, 2011 at 12:40


  • 2

    you can also do: git diff –name-only [email protected]{3} [email protected]{0} for the exact commits you want to compare.

    – b01

    Nov 29, 2011 at 16:18


  • 8

    @AugustLilleaas actually using show will only show the 2 specific commits, if you have commits between those 2 they will be left out

    – chrisan

    Oct 10, 2012 at 17:03

  • 4

    As noted below, git diff --name-status doesn’t seem to want to show added files. @sschuberth pointed out git show, which does seem to work properly for me: git show --pretty=format: --name-status. Just doing git show --name-status gives a bit more info, but still nice and dense… that will be my new goto command 😉

    – travc

    Jun 14, 2017 at 22:52


536

git diff --name-status [SHA1 [SHA2]]

is like –name-only, except you get a simple prefix telling you what happened to the file (modified, deleted, added…)

git log --name-status --oneline [SHA1..SHA2]

is similar, but commits are listed after the commit message, so you can see when a file was changed.

  • if you’re interested in just what happened to certain files/folders you can append -- <filename> [<filename>...] to the git log version.

  • if you want to see what happened for a single commit, call it SHA1, then do
    git log --name-status --oneline [SHA1^..SHA1]

File status flags:

FlagNameMeaning
MmodifiedFile has been modified
Ccopy-editFile has been copied and modified
Rrename-editFile has been renamed and modified
AaddedFile has been added
DdeletedFile has been deleted
UunmergedFile has conflicts after a merge

7

  • I happen to say git diff –name-status and it did give the ‘added file’.

    – aartist

    Feb 7, 2013 at 20:17

  • 1

    For git log, it needs to have two dots between the SHAs, like SHA1..SHA2, and the second SHA isn’t optional, so it should look like this: git log –name-status –oneline [SHA1..SHA2]

    May 13, 2014 at 1:27


  • 3

    The --relative[=<path>] option may help you, I’m not sure. Otherwise there’s always | erep -v '(.tmp|.foo|.dontwant)$'

    Oct 15, 2014 at 15:08

  • 1

    @artfulrobot did you mean egrep?

    May 5, 2021 at 19:18

  • 1

    @DavidMoles yes I did, but I can’t edit the comment now!

    May 6, 2021 at 9:27

132

It seems that no one has mentioned the switch --stat:

$ git diff --stat HEAD~5 HEAD
 .../java/org/apache/calcite/rex/RexSimplify.java   | 50 +++++++++++++++++-----
 .../apache/calcite/sql/fun/SqlTrimFunction.java    |  2 +-
 .../apache/calcite/sql2rel/SqlToRelConverter.java  | 16 +++++++
 .../org/apache/calcite/util/SaffronProperties.java | 19 ++++----
 .../org/apache/calcite/test/RexProgramTest.java    | 24 +++++++++++
 .../apache/calcite/test/SqlToRelConverterTest.java |  8 ++++
 .../apache/calcite/test/SqlToRelConverterTest.xml  | 15 +++++++
 pom.xml                                            |  2 +-
 .../apache/calcite/adapter/spark/SparkRules.java   |  7 +--
 9 files changed, 117 insertions(+), 26 deletions(-)

There are also --numstat

$ git diff --numstat HEAD~5 HEAD
40      10      core/src/main/java/org/apache/calcite/rex/RexSimplify.java
1       1       core/src/main/java/org/apache/calcite/sql/fun/SqlTrimFunction.java
16      0       core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
8       11      core/src/main/java/org/apache/calcite/util/SaffronProperties.java
24      0       core/src/test/java/org/apache/calcite/test/RexProgramTest.java
8       0       core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
15      0       core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
1       1       pom.xml
4       3       spark/src/main/java/org/apache/calcite/adapter/spark/SparkRules.java

and --shortstat

$ git diff --shortstat HEAD~5 HEAD
9 files changed, 117 insertions(+), 26 deletions(-)

2

  • 12

    The accepted answer is correct, but this is super useful and gives you a little extra info. Thanks!

    – kontur

    Jan 8, 2018 at 10:33

  • 3

    Agreed this is a more useful answer since it contains the diff stats.

    Apr 30, 2018 at 19:48