• Uncategorized

About git : How-do-I-list-all-the-files-in-a-commit

Question Detail

How do I print a plain list of all files that were part of a given commit?

Although the following lists the files, it also includes unwanted diff information for each:

git show a303aa90779efdd2f6b9d90693e2cbbbe4613c1d

Question Answer

Preferred Way (because it’s a plumbing command; meant to be programmatic):

$ git diff-tree --no-commit-id --name-only -r bd61ad98

Another Way (less preferred for scripts, because it’s a porcelain command; meant to be user-facing)

$ git show --pretty="" --name-only bd61ad98    

  • The --no-commit-id suppresses the commit ID output.
  • The --pretty argument specifies an empty format string to avoid the cruft at the beginning.
  • The --name-only argument shows only the file names that were affected (Thanks Hank). Use --name-status instead, if you want to see what happened to each file (Deleted, Modified, Added)
  • The -r argument is to recurse into sub-trees

If you want to get the list of changed files:

git diff-tree --no-commit-id --name-only -r <commit-ish>

If you want to get the list of all files in a commit, you can use

git ls-tree --name-only -r <commit-ish>

I’ll just assume that gitk is not desired for this. In that case, try git show --name-only <sha>.

I personally use the combination of –stat and –oneline with the show command:

git show --stat --oneline HEAD
git show --stat --oneline b24f5fb
git show --stat --oneline HEAD^^..HEAD

If you do not like/want the addition/removal stats, you can replace –stat with –name-only

git show --name-only --oneline HEAD
git show --name-only --oneline b24f5fb
git show --name-only --oneline HEAD^^..HEAD

You can also do

git log --name-only

and you can browse through various commits, commit messages and the changed files.

Type q to get your prompt back.

Recently I needed to list all changed files between two commits. So I used this (also *nix specific) command

git show --pretty="format:" --name-only START_COMMIT..END_COMMIT | sort | uniq

Or as Ethan points out:

git diff --name-only START_COMMIT..END_COMMIT

Using --name-status will also include the change (added, modified, deleted, etc.) next to each file:

git diff --name-status START_COMMIT..END_COMMIT

Simplest form:

git show --stat (hash)

That’s easier to remember and it will give you all the information you need.

If you really want only the names of the files you could add the --name-only option.

git show --stat --name-only (hash)

I use the changed alias quite often. To set it up:

git config --global alias.changed 'show --pretty="format:" --name-only'


git changed (lists files modified in last commit)
git changed bAda55 (lists files modified in this commit)
git changed bAda55..ff0021 (lists files modified between those commits)

Similar commands that may be useful:

git log --name-status --oneline (very similar, but shows what actually happened M/C/D)
git show --name-only


git log --name-status

This will show you the commit id, message, the files changed and whether it was modified, created, added, or deleted. Somewhat of an all-in-one command.

Using the standard git diff command (also good for scripting):

git diff --name-only <sha>^ <sha>

If you also want the status of the changed files:

git diff --name-status <sha>^ <sha>

This works well with merge commits.

Try this command for name and changes number of lines

git show --stat <commit-hash>

Only show file names

git show --stat --name-only  <commit-hash>

For getting the last commit hash, try this command:

git log -1

Last commit with show files name and file status modify, create, or delete:

 git log -1 --oneline --name-status <commit-hash>

Or for all

git log

For more advanced git log information, read these articles:

  • Git log format string cheat sheet

  • git log cheat sheet

To list the files changed on a particular commit:

git show --pretty=%gd --stat <commit_id>

To list the files changed on recent commit:

git show --pretty=%gd --stat

$ git log 88ee8^..88ee8 --name-only --pretty="format:"

OK, there are a couple of ways to show all files in a particular commit…

To reduce the information and show only names of the files which committed, you simply can add --name-only or --name-status flag… These flags just show you the file names which are different from previous commits as you want…

So you can do git diff followed by --name-only, with two commit hashes after <sha0> <sha1>. Something like below:

git diff --name-only 5f12f15 kag9f02

I also created the below image to show all steps to go through in these situations:

Use a simple one-line command, if you just want the list of files changed in the last commit:

git diff HEAD~1 --name-only

There’s also git whatchanged, which is more low level than git log

       git-whatchanged - Show logs with difference each commit introduces

It outputs the commit summary with a list of files beneath it with their modes and if they were added(A), deleted(D), or modified(M);

$ git whatchanged f31a441398fb7834fde24c5b0c2974182a431363

Would give something like:

commit f31a441398fb7834fde24c5b0c2974182a431363
Author: xx <[email protected]>
Date:   Tue Sep 29 17:23:22 2015 +0200

    added fb skd and XLForm

:000000 100644 0000000... 90a20d7... A  Pods/Bolts/Bolts/Common/BFCancellationToken.h
:000000 100644 0000000... b5006d0... A  Pods/Bolts/Bolts/Common/BFCancellationToken.m
:000000 100644 0000000... 3e7b711... A  Pods/Bolts/Bolts/Common/BFCancellationTokenRegistration.h
:000000 100644 0000000... 9c8a7ae... A  Pods/Bolts/Bolts/Common/BFCancellationTokenRegistration.m
:000000 100644 0000000... bd6e7a1... A  Pods/Bolts/Bolts/Common/BFCancellationTokenSource.h
:000000 100644 0000000... 947f725... A  Pods/Bolts/Bolts/Common/BFCancellationTokenSource.m
:000000 100644 0000000... cf7dcdf... A  Pods/Bolts/Bolts/Common/BFDefines.h
:000000 100644 0000000... 02af9ba... A  Pods/Bolts/Bolts/Common/BFExecutor.h
:000000 100644 0000000... 292e27c... A  Pods/Bolts/Bolts/Common/BFExecutor.m
:000000 100644 0000000... 827071d... A  Pods/Bolts/Bolts/Common/BFTask.h

I know this answer doesn’t really match “with no extraneous information.”, but I still think this list is more useful than just the filenames.

I use this to get the list of changed files in a merge commit

λ git log -m -1 --name-only --pretty="format:"


λ git log -m -1 --name-status --pretty="format:"
A       configs/anotherconfig.xml
M       configs/configsInRepo.xml

I use this to get the list of modified files between two changesets:

git diff --name-status <SHA1> <SHA2> | cut -f2

I like to use

git show --stat <SHA1>^..<SHA2>

I found a perfect answer to this:

git show --name-status --oneline <commit-hash>

So that I can know

  • which files were just modified (M)

  • Which files were newly added (A)

  • Which files were deleted (D)

I like this:

git diff --name-status <SHA1> <SHA1>^

Display the log.

COMMIT can be blank (“”), the SHA-1 hash, or a shortened version of the SHA-1 hash.

git log COMMIT -1 --name-only

This will list just the files and is very useful for further processing.

git log COMMIT -1 --name-only --pretty=format:"" | grep "[^\s]"

List the files that changed in a commit:

git diff --name-only SHA1^ SHA1

This doesn’t show log messages, extra newlines, or any other clutter. This works for any commit, not just the current one.

Only the file list (not even commit message):

git show --name-only --pretty=format:

E.g. open all changed files in your editor:

git show --name-only --pretty=format: | xargs "$EDITOR"

A combination of “git show --stat” (thanks Ryan) and a couple of sed commands should trim the data down for you:

git show --stat <SHA1> | sed -n "/ [\w]\*|/p" | sed "s/|.\*$//"

That will produce just the list of modified files.

git show --name-only a303aa90779efdd2f6b9d90693e2cbbbe4613c1d

There is a simple trick to view as a file listing. Just add : after the hash:

git show 9d3a52c474:

You can then drill in,

git show 9d3a52c474:someDir/someOtherDir

If you hit a file, you’ll get the raw version of the file; which sometimes is what you want if you’re only looking for a nice reference or key pieces of code (diffs can make everything a mess),

git show 9d3a52c474:someDir/someOtherDir/somefile

The only drawback of this method is that it doesn’t easily show a tree of files.

List all files in a commit tree:

git ls-tree --name-only --full-tree a21e610

git show [email protected]{0}

works fine for me

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.