2422

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?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Shawn
  • 30,399
  • 17
  • 44
  • 72

15 Answers15

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Peter
  • 121,125
  • 53
  • 174
  • 208
  • 179
    This works for git show as well. `git show --name-only SHA1`. – August Lilleaas Nov 23 '09 at 13:11
  • 99
    `git diff --name-status [TAG|SHA1]` shows what operations were done to the files too – reconbot Sep 28 '11 at 12:40
  • 2
    you can also do: git diff --name-only HEAD@{3} HEAD@{0} for the exact commits you want to compare. – b01 Nov 29 '11 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 '12 at 17:03
  • 1
    not what the OP asked, but you can also do: `git log -p --name-only` – Lenny Sirivong Mar 21 '13 at 00:23
  • `--name-only` only shows the *files* that were changed.. now If I do `git diff SHA1 SHA2` I'll see all the code.. for large commits this can look really bad on the CLI.. is there a gui software (ie a diff tool like [diffmerge](https://sourcegear.com/diffmerge/)) that can accept such a git command and visually display the diff? – abbood Dec 13 '13 at 06:40
  • never mind.. i just figured out that it can be done visually.. ie with tools like [sourcetree](https://www.atlassian.com/software/sourcetree/overview?_mid=8b90e14347096994af7257f4bfdf89e0&gclid=COrPx5jNrLsCFSYHwwodZAcAXQ) using diffmerge as a gui diff app.. you can simply select all the commits (including the one before the first commit, or else you won't see the changes of the first commit).. and it will aggregate all the diffs in a nice way.. pretty neat! – abbood Dec 13 '13 at 06:47
  • this will show only the file name not the changes in the files, check my response below: `git diff --word-diff SHA1 SHA2` – Julio Marins Dec 04 '14 at 15:11
  • 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 '17 at 22:52
  • That's pretty cool, how can you filter by author then like you do with `git log --author`? – tonix Nov 19 '18 at 20:46
  • `git diff --name-only HEAD^` for what changed in the most recent commit – starscream_disco_party May 23 '19 at 15:59
  • By the way, just like `git status` shows a list of modified files, you can do `git diff --name-only` to get the same list without the *modified:* prefix. Useful if you want to pipe the output for processing. – Nagev Jun 25 '19 at 13:58
  • Is there some way to colorize this output? – Unknow0059 Jan 12 '21 at 14:58
525
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
muru
  • 4,489
  • 1
  • 32
  • 73
artfulrobot
  • 19,213
  • 11
  • 49
  • 74
130

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(-)
leeyuiwah
  • 6,006
  • 4
  • 36
  • 62
68

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`
Tim James
  • 1,413
  • 1
  • 14
  • 15
  • 1
    This was really useful! I wish I could simply say `git diffstatus master` or similar, that triggers the above. – oma Jun 04 '13 at 12:04
  • 3
    Or `git show --pretty=format: --name-only origin/master..`. – sschuberth Apr 07 '14 at 19:11
  • You might not be able to make it a git alias, but you can definitely put it into your .bashrc. – Fred Feb 25 '16 at 22:24
  • 3
    Or even simpler: `git diff --name-only HEAD...master` (note the three dots). For a detailed explanation, see [here](http://stackoverflow.com/a/7256391/2063031). – ostrokach Dec 26 '16 at 07:47
  • 1
    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) – x'ES May 19 '17 at 13:08
31

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.

halfer
  • 19,471
  • 17
  • 87
  • 173
Max MacLeod
  • 25,335
  • 12
  • 98
  • 129
  • 5
    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) – Jasper Feb 19 '15 at 10:13
  • this won't work if there are `"A\t"` in the file names. You need `grep "^A\t"` – phuclv Nov 04 '20 at 02:14
12

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
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Parris
  • 17,121
  • 16
  • 86
  • 129
10

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'
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Zorayr
  • 22,104
  • 6
  • 124
  • 114
  • 3
    Or `git config --global alias.showfiles 'show --pretty="format:" --name-only'` to make `git showfiles`. – cgmb Oct 09 '17 at 08:50
8

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
BanksySan
  • 25,929
  • 29
  • 105
  • 202
m12lrpv
  • 506
  • 1
  • 6
  • 13
7

This will show the changes in files:

git diff --word-diff SHA1 SHA2
Julio Marins
  • 9,225
  • 8
  • 43
  • 52
5

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
blindsnowmobile
  • 3,308
  • 6
  • 30
  • 43
4

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

 git diff --name-status SHA1 SHA2 | grep '\.java$'
K. Symbol
  • 2,481
  • 15
  • 21
3

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Agni
  • 408
  • 5
  • 15
3

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
girishso
  • 331
  • 2
  • 6
2

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
2

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
Jaime Montoya
  • 5,968
  • 7
  • 60
  • 85