如何列出提交中的所有文件?

我正在寻找一个简单的 Git 命令,该命令提供格式良好的所有文件列表,这些文件是由哈希 (SHA-1) 给出的提交的一部分,没有无关信息。

我试过了:

git show a303aa90779efdd2f6b9d90693e2cbbbe4613c1d

虽然它列出了文件,但它也包含每个文件不需要的差异信息。

是否有另一个 git 命令将只提供我想要的列表,这样我就可以避免从 git show 输出中解析它?

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

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.