1

I need to get a list of all the changed files from the initial commit until the latest one.

So, here is my case:

$ git init .
$ echo "test1" > test1
$ git add test1
$ git commit -m "test 1" 
$ echo "test2" > test2
$ git add test2
$ git commit -m "test 2"
$ echo "test3" > test3
$ git add test3
$ git commit -m "test 3"

$ git log 
commit 13b72d5468dfbb9582ad4d225cf1e6e8bb17f1bc (HEAD -> master)
Author: carlspring <...>
Date:   Fri Feb 12 18:58:58 2021 +0000

    test 3

commit 6955a875fdf57efaa3859827b7f975fa26095d38
Author: carlspring <...>
Date:   Fri Feb 12 18:58:56 2021 +0000

    test 2

commit 9410cf6a98986794c8ffe38c15cc60eaa054705c
Author: carlspring <...>
Date:   Fri Feb 12 18:58:56 2021 +0000

    test 1
carlspring@carlspring:/tmp/test$ git log --all --full-history --pretty=%H
13b72d5468dfbb9582ad4d225cf1e6e8bb17f1bc
6955a875fdf57efaa3859827b7f975fa26095d38
9410cf6a98986794c8ffe38c15cc60eaa054705c

I would like to get a list that produces all the changed files since the first commit and look like this:

test1
test2
test3

Now, I've obviously seen all the answers here that recommend using git diff-tree, so tried it:

$ git diff-tree --no-commit-id --name-only -r 9410cf6a98986794c8ffe38c15cc60eaa054705c..HEAD
test2
test3

Why am not getting test1 as well?

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • `test1` is already part of the first commit, so it's not a file changed "between the first commit and the HEAD". You could have started with an empty commit at the very start and add `test1` in a separate commit, then itwould show up. – Joachim Sauer Feb 12 '21 at 19:23
  • @JoachimSauer: Well, this will eventually be executed against a repository with actual history. How would one go about doing this in such a case? – carlspring Feb 12 '21 at 19:25
  • you can just create a new empty commit without a parent and since this only compares `tree` objects it shouldn't care that the two don't share any history. So your synthetic empty commit can be compared to the HEAD and it'll list all the files that are different (which will be all the files in HEAD, by definition). – Joachim Sauer Feb 12 '21 at 19:27
  • To whomever decided to close this as a duplicate: it actually isn't. The answer you've linked to, shows the diff, not the list of all changed files. – carlspring Feb 13 '21 at 14:13

1 Answers1

1

You should be able to do it by using the git empty tree id:

$ git diff --name-only  4b825dc642cb6eb9a060e54bf8d69288fbee4904 HEAD
test1
test2
test3
tftd
  • 16,203
  • 11
  • 62
  • 106
  • @carlspring It's short and to the piont; but you said the real repo will have history, and this won't list *just the files your commits changed* in that situation. – Mark Adelsberger Feb 12 '21 at 19:45
  • 1
    I'm not sure I follow what you're saying @MarkAdelsberger. It lists the list of changed files before the initial commit and the current `HEAD`. It looks like this is what's required based on the given example. :) – tftd Feb 12 '21 at 19:51
  • @tftd What I'm saying is that OP wrote a comment in which they said that another solution equivalent to this would not work because "this will eventually be executed against a repository with actual history." What you've provided doens't show changes; it shows the full contents of the final commit. – Mark Adelsberger Feb 12 '21 at 20:51
  • 1
    `git commit --allow-empty` as @Joachim suggest is not the same as `git diff --name-only 4b825dc642cb6eb9a060e54bf8d69288fbee4904 HEAD`. The first command will actually **create a commit** and the second one will not. As for the quoted comment I feel like you might have misunderstood it. How would you `start with an empty commit at the very start and add test1 in a separate commit` when you have an existing repo? It would only work for newly created repositories? My understanding was based on the example which suggests he wants the full list of changed files `empty tree..HEAD`. :) – tftd Feb 12 '21 at 21:47