乐闻世界logo
搜索文章和话题

How do i list all the files in a commit?

2个答案

1
2

How to list all files in a commit? To list all files in a Git commit, you can use the git show command with the --name-only or --name-status option. Here's how to do it:

  1. Using the --name-only option:
sh
git show --name-only <commit-hash>

<commit-hash> is the hash of the commit you want to inspect. This command lists all file names that were changed (including additions and deletions) in that commit.

  1. Using the --name-status option:
sh
git show --name-status <commit-hash>

This command not only lists the file names but also shows the status of each file, such as M for Modified, A for Added, and D for Deleted.

If you only want to obtain the file list without seeing other commit details (such as diff or commit message), you can use --pretty=format: to suppress additional output:

sh
git show --pretty=format: --name-only <commit-hash>

Or:

sh
git show --pretty=format: --name-status <commit-hash>

If you don't know the commit hash but know it's the most recent commit, you can use HEAD to reference the latest commit, or HEAD~1 for the previous commit, and so on. For example, to list all files in the most recent commit:

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

Additionally, if you want to view which files were modified in the last commit of a specific branch or tag, replace HEAD with the branch name or tag name. For example, to view the last commit of the feature-branch branch:

sh
git show --pretty=format: --name-only feature-branch

Using these methods, you can easily view all files included in a Git commit.

2024年6月29日 12:07 回复

Preferred method (as it is a pipeline command designed for scripting): $ git diff-tree --no-commit-id --name-only bd61ad98 -r index.html javascript/application.js javascript/ie6.js

Alternative method (less favored for scripting as it is an interactive command meant for user interaction): $ git show --pretty="" --name-only bd61ad98 index.html javascript/application.js javascript/ie6.js


  • Suppresses the output of commit IDs.
  • The --pretty parameter specifies an empty format string to avoid clutter at the beginning.
  • The --name-only parameter only shows the affected filenames (thanks to Hank). If you want to see what happened to each file (delete, modify, add), use --name-status instead.
  • The -r parameter recurses into subtrees.
2024年6月29日 12:07 回复

你的答案