Categories
git git-show

How do I list all the files in a commit?

3322

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

4

  • 54

    I came here looking for something a bit different. I want to see all files modified for a set of commits and wound up using git log --until 2013-05-21 --pretty="short" --name-only with a good effect.

    Dec 12, 2013 at 17:40


  • 8

    Use this command to get all changes from previous n commits till master: git diff-tree --name-status -r @{3} master

    – ako

    Jun 22, 2018 at 13:57

  • 5

    git diff --name-only master – To list ALL changed files on current branch, comparing to master branch.

    Jun 30, 2019 at 9:39

  • You can check this answer out: stackoverflow.com/questions/17563726/…

    Mar 9 at 20:36

4464

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

$ git diff-tree --no-commit-id --name-only -r bd61ad98
index.html
javascript/application.js
javascript/ie6.js

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

$ git show --pretty="" --name-only bd61ad98    
index.html
javascript/application.js
javascript/ie6.js

  • 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

30

  • 37

    It should be noted that diff-tree won’t work when looking at the root commit.

    Mar 6, 2013 at 5:52

  • 362

    Replacing the --name-only option with --name-status will give more clear summary.

    Apr 11, 2013 at 3:58

  • 33

    If you want it to work on the root commit, use the –root flag. From the man page: “When –root is specified the initial commit will be shown as a big creation event. This is equivalent to a diff against the NULL tree.”

    – Chris

    Sep 23, 2013 at 18:26

  • 28

    git log --name-only -n 1 <hash> The last commit would be: git log --name-only -n 1 HEAD~1..HEAD

    – Kurt

    Nov 11, 2013 at 4:20


  • 20

    If anyone is wondering (like I was) why the first way is “preferred,” it goes back to @drizzt ‘s comment; git show is “porcelain” (meant to be user facing) and git diff-tree is “plumbing” (meant to be used programmatically, e.g. from scripts). The interface for the former may change over time (so the git maintainers could drop --name-only although I don’t imagine they would) for useability reasons, whereas the interface for the latter will be kept as stable as possible for compatibility reasons.

    Jun 29, 2015 at 4:42


316

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>

13

  • 1

    The ls-tree with –name-only does not seem to work on 1.6.4.4 or 1.6.3.3. Do you think this is a bug ?

    Oct 10, 2009 at 10:20

  • 2

    It turns out the ordering of the parameters is significant here. The one in your post does not work, while the one in your response does work – at least until you update your post 😉

    Oct 10, 2009 at 15:17

  • 5

    Pass --no-commit-id to avoid printing the SHA1, like so: git diff-tree --no-commit-id --name-only -r <commit-ish>

    Aug 15, 2012 at 20:00

  • 1

    diff-tree doesn’t return anything, possibly because the commit I refer to added files, but not changed any existing. ls-tree gives me a list of all files in the repo, not just the files added/altered in the last commit. I wonder if there’s a 3rd command to just list all new files (i.e. added in that given commit)?

    – CodeManX

    Nov 17, 2015 at 23:03


  • 3

    @CoDEmanX : You didn’t miss adding -r / -t option, did you? Because diff-tree handles both modified and added files. If you want to list all new (added) files, use git diff-tree -r --name-only --no-commit-id --diff-filter=A <commit-ish>

    Nov 17, 2015 at 23:15


277

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

3

  • 41

    –name-only is plenty in most cases where i needed it; Therefore, upvoted the shortest solution (and the only one that i’d remember in 1 try).

    – Erik S

    Aug 1, 2012 at 21:42

  • As someone who really likes CLI git, gitk is actually a decent way of reviewing the files and displaying the file that the diff is on. e.g. Code reviewing a monster commit from a peer.

    Oct 27, 2017 at 21:56

  • 3

    The shortest and easiest answer to remember.

    Jun 24, 2020 at 8:39