0

If I have a given commit SHA, what is the best way to show only the files that were added through that commit? Thanks.

Monwe
  • 677
  • 1
  • 5
  • 7
  • 1
    possible duplicate of [List all the files for a commit in Git](http://stackoverflow.com/questions/424071/list-all-the-files-for-a-commit-in-git) – lurker Oct 19 '13 at 20:57
  • That answer didn't specify how to filter the commit for only files added through the commit. – Monwe Oct 19 '13 at 21:11
  • How about `git show --name-status SHA` or `git show --name-only SHA`? – lurker Oct 19 '13 at 21:15
  • That would work but doesn't seem to handle files added through a merge commit. – Monwe Oct 19 '13 at 21:18

2 Answers2

3

"Best" is tricky (what's the best flavor of ice cream?) but there are a lot of ways.

Shortest output, but assumes that the commit is not a merge [edit: or, well, see below]:

git diff --name-only --diff-filter=A <id>^ <id>

Easiest way to use this is probably with a git alias:

git config alias.files-added '!git diff --name-only --diff-filter=A $1^'

(note that running git files-added with no arguments will give a weird "bad revision" message, but oh well).

If you need to handle merge commits, you have to decide what it means to "add a file". Is the file added if it was not in any parent? Is it added if it was not in all parents? For instance, suppose you've merged subtopic-A, subtopic-B, and subtopic-C into topic, and file xyz was present in both subtopic-B and topic, but was not present in either subtopic-A or subtopic-C. Was xyz "added"?

Edit: if the definition of "added" means "was not in what most would call the ‘main line’ before the merge", this same command would work. That is, if you're on branch topic as above, and you merge one or more other branches into topic and the final merge commit contains file galumph when topic did not have a file named galumph before, this would be considered "added" (no matter which, if any, of the merged-in branches had it).

torek
  • 448,244
  • 59
  • 642
  • 775
0

After a bit more research this seems to be a pretty reliable solution.

git show -m --name-status -1 -U | grep $'A\t'

Monwe
  • 677
  • 1
  • 5
  • 7
  • Note that this answers the "what does it mean to add a file" question with "if it was not in *any* parent", e.g., if you merge `feature` into `master` and `master` has a file `yoink` that is not in `feature`, you'll see it (even though `master` might be considered the "main line" here). – torek Oct 19 '13 at 21:43