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

Asked 2023-09-20 20:27:30 View 327,814

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?

  • For a commit and its parent: stackoverflow.com/questions/424071/… - anyone
  • You should change the question title...you don't want to list the file names that changed...you want to list the names of files that changed. Note that it is possible to change the names of files without changing their contents. - anyone

Answers

git diff --name-only SHA1 SHA2

where you only need to include enough of the SHA hash to identify the commits. The order of the SHAs does not matter. The output (which includes the relative path, not just the file name) follows this format:

 dir 1/dir 2/filename.ext
 dir 3/dir 4/other filename.ext

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).

Answered   2023-09-20 20:27:30

  • This works for git show as well. git show --name-only SHA1. - anyone
  • git diff --name-status [TAG|SHA1] shows what operations were done to the files too - anyone
  • you can also do: git diff --name-only HEAD@{3} HEAD@{0} for the exact commits you want to compare. - anyone
  • @AugustLilleaas actually using show will only show the 2 specific commits, if you have commits between those 2 they will be left out - anyone
  • 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 ;) - anyone
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:

Flag Name Meaning
M modified File has been modified
C copy-edit File has been copied and modified
R rename-edit File has been renamed and modified
A added File has been added
D deleted File has been deleted
U unmerged File has conflicts after a merge

Answered   2023-09-20 20:27:30

  • I happen to say git diff --name-status and it did give the 'added file'. - anyone
  • 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] - anyone
  • The --relative[=<path>] option may help you, I'm not sure. Otherwise there's always | erep -v '(.tmp|.foo|.dontwant)$' ... - anyone
  • @artfulrobot did you mean egrep? - anyone
  • @DavidMoles yes I did, but I can't edit the comment now! - anyone

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(-)

Answered   2023-09-20 20:27:30

  • The accepted answer is correct, but this is super useful and gives you a little extra info. Thanks! - anyone
  • Agreed this is a more useful answer since it contains the diff stats. - anyone

But for seeing the files changed between your branch and its common ancestor with another branch (say origin/master):

git diff --name-only `git merge-base origin/master HEAD`

Answered   2023-09-20 20:27:30

  • This was really useful! I wish I could simply say git diffstatus master or similar, that triggers the above. - anyone
  • Or git show --pretty=format: --name-only origin/master... - anyone
  • You might not be able to make it a git alias, but you can definitely put it into your .bashrc. - anyone
  • Or even simpler: git diff --name-only HEAD...master (note the three dots). For a detailed explanation, see here. - anyone
  • Looks like mostly correct answer! Simple git diff --name-only master..branch doesn't correspond to github PR list. This way more precise. But anyway I have 173 chaned files vs 171 in github PR. (without merge-base I have 228 vs 171) - anyone

The biggest issue with every previous answer is that you get fed into a pager which is extremely annoying if you want to use the information you're trying to get out of the repository. Especially if you're a developer that would rather be learning the business logic of the application your supposed to be developing instead of learning vim commands.

Using --no-pager solves that issue.

git --no-pager diff --name-only sha1 sha2

Answered   2023-09-20 20:27:30

  • The --no-pager is really key. Thanks for standing out. - anyone
  • An alternative to the --no-pager option is simply to pipe to cat (though you do lose the colour): git diff --name-only sha1 sha2 | cat - anyone
  • Set once and for all in git config: core.pager=less -+X -F - screen-shorter output will go in the console, long output (requiring scrolling) will show up in the pager, indispensable. - anyone

To supplement @artfulrobot's answer, if you want to show changed files between two branches:

git diff --name-status mybranch..myotherbranch

Be careful on precedence. If you place the newer branch first then it would show files as deleted rather than added.

Adding a grep can refine things further:

git diff --name-status mybranch..myotherbranch | grep "A\t"

That will then show only files added in myotherbranch.

Answered   2023-09-20 20:27:30

  • Regexes are nice an can indeed do almost anything. In this case, though, there's also --diff-filter which gives this functionality natively, which means less chance of incorrect results (e.g. false positives) - anyone
  • this won't work if there are "A\t" in the file names. You need grep "^A\t" - anyone

Also note, if you just want to see the changed files between the last commit and the one before it, this works fine:

git show --name-only

Answered   2023-09-20 20:27:30

  • simple and easy to remember. It should have get a lot more upvotes. - anyone

Add the below alias to your ~/.bash_profile file, and then run source ~/.bash_profile; now anytime you need to see the updated files in the last commit, run, showfiles from your git repository.

alias showfiles='git show --pretty="format:" --name-only'

Answered   2023-09-20 20:27:30

  • Or git config --global alias.showfiles 'show --pretty="format:" --name-only' to make git showfiles. - anyone

The following works well for me:

git show --name-only --format=tformat: SHA1..SHA2

It can also be used with a single commit:

git show --name-only --format=tformat: SHA1

which is handy for use in Jenkins where you are provided with a list of changeset SHA hash values, and want to iterate over them to see which files have been changed.

This is similar to a couple of the previous answers, but using tformat: rather than format: removes the separator space between commits.

Answered   2023-09-20 20:27:30

This will show the changes in files:

git diff --word-diff SHA1 SHA2

Answered   2023-09-20 20:27:30

Just for someone who needs to focus only on Java files, this is my solution:

 git diff --name-status SHA1 SHA2 | grep '\.java$'

Answered   2023-09-20 20:27:30

In case someone is looking for the list of changed files, including staged files

git diff HEAD --name-only --relative --diff-filter=AMCR

git diff HEAD --name-only --relative --diff-filter=AMCR sha-1 sha-2

Remove --relative if you want absolute paths.

Answered   2023-09-20 20:27:30

  • diff-filter=... was what I was searching - anyone

Use

git log --pretty=oneline > C:\filename.log

which will log only a oneline (--pretty=oneline) that's the name of the changed file. It will also log all the details to your output file.

Answered   2023-09-20 20:27:30

  • git log --pretty=oneline gives me only the SHA and the commit message using git 2.10.1 - anyone
  • On Windows? In what context? - anyone

Based on git diff --name-status I wrote the git-diffview Git extension that renders a hierarchical tree view of what changed between two paths.

Answered   2023-09-20 20:27:30

As artfulrobot said in his answer:

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

My example:

git diff --name-status 78a09k12067c24d8f117886c4723ccf111af4997 
4b95d595812211553070046bf2ebd807c0862cca
M       views/layouts/default.ctp
M       webroot/css/theme.css
A       webroot/img/theme/logo.png

Answered   2023-09-20 20:27:30

  • This adds nothing to the previous answers. - anyone