如何列出Git提交中的新增文件?提交过滤失效问题求助
git whatchanged returning all commits instead of your target one Got it, I see the issue here—you're trying to list only the newly added files in a specific Git commit, but your current command is spitting out every commit in history instead of just bc895ad6a411683f5737d4a4b89e52f54cd2d68c. Let's fix this quickly:
Correct git whatchanged Command
The problem comes down to parameter order and missing a flag to limit the commit scope. Here's the adjusted command that works:
git whatchanged -1 --diff-filter=A --pretty=format: bc895ad6a411683f5737d4a4b89e52f54cd2d68c
Breakdown of the Fix:
-1: Tells Git to only show 1 commit (your target one), instead of traversing all history from that commit backwards--diff-filter=A: Filters output to only include newly added files--pretty=format:: Clears out commit metadata so you only see the list of files- The commit ID goes at the end: Explicitly tells Git which commit to inspect
Simpler Alternatives
If you don't strictly need to use git whatchanged, these commands are more straightforward for your use case:
Option 1: git show (most user-friendly)
git show --diff-filter=A --name-only bc895ad6a411683f5737d4a4b89e52f54cd2d68c
--name-only strips away all diff content and only shows the filenames, making the output clean and focused on what you need.
Option 2: git diff-tree (great for scripts)
This is a lower-level command that's perfect if you want to parse the output programmatically:
git diff-tree --no-commit-id --name-only --diff-filter=A -r bc895ad6a411683f5737d4a4b89e52f54cd2d68c
-r: Recursively checks files in subdirectories--no-commit-id: Omits the commit ID from the output--name-only: Only lists filenames
Any of these should give you exactly the list of newly added files in your target commit!
内容的提问来源于stack exchange,提问作者Alexandre Queiroz de Oliveira

