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

GitGit DiffGit Show

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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`

Solution 5 - Git

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.

Solution 6 - Git

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

Solution 7 - Git

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'

Solution 8 - Git

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

Solution 9 - Git

This will show the changes in files:

git diff --word-diff SHA1 SHA2

Solution 10 - Git

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.

Solution 11 - Git

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

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

Solution 12 - Git

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.

Solution 13 - Git

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.

Solution 14 - Git

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

Solution 15 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShawnView Question on Stackoverflow
Solution 1 - GitPeterView Answer on Stackoverflow
Solution 2 - GitartfulrobotView Answer on Stackoverflow
Solution 3 - GitleeyuiwahView Answer on Stackoverflow
Solution 4 - GitTim JamesView Answer on Stackoverflow
Solution 5 - GitMax MacLeodView Answer on Stackoverflow
Solution 6 - GitParrisView Answer on Stackoverflow
Solution 7 - GitZorayrView Answer on Stackoverflow
Solution 8 - Gitm12lrpvView Answer on Stackoverflow
Solution 9 - GitJulio MarinsView Answer on Stackoverflow
Solution 10 - GitblindsnowmobileView Answer on Stackoverflow
Solution 11 - GitK. SymbolView Answer on Stackoverflow
Solution 12 - GitAgniView Answer on Stackoverflow
Solution 13 - Gituser3136493View Answer on Stackoverflow
Solution 14 - GitJaime MontoyaView Answer on Stackoverflow
Solution 15 - GitgirishsoView Answer on Stackoverflow