Polo
Polo

Reputation: 129

How to list files by when it was first added to a Git repo?

How do you find the most recently added files in git, on a Linux OS?

Using git log on every file is not feasible.

And git ls-files shows the full list of files, but I don't see any dates associated with them.

Upvotes: 2

Views: 368

Answers (3)

phd
phd

Reputation: 94483

git log --name-status --diff-filter=A

See the docs about --diff-filter.

Upvotes: 2

Polo
Polo

Reputation: 129

Creation dates, but it still takes a long time:

for file in $(git ls-files); do echo -n "$file "; git log --format=%ai $file | tail -1; done

Last modified dates is the same but use head instead of tail:

for file in $(git ls-files); do echo -n "$file "; git log --format=%ai $file | tail -1; done

Upvotes: 0

larsks
larsks

Reputation: 311606

You don't need to run git log on "every file". You only need to run it once. Use something like git log --name-status --format=%H which produces output like this:

aa56a2ff6a56697342908cc0cc85a537ecea4325

A   changelogs/fragments/70887_galaxy_token.yml
M   lib/ansible/galaxy/token.py
932ba3616067007fd5e449611a34e7e3837fc8ae

M   lib/ansible/module_utils/basic.py
M   lib/ansible/modules/get_url.py
A   test/integration/targets/unsafe_writes/aliases
A   test/integration/targets/unsafe_writes/basic.yml
[...]

For each commit, this lists the files involved and their status, where M is "modified", A is added, etc.

Now just look for lines that start with A<tab>:

$ git log --name-status --format=%H | awk -F'\t' '$1 == "A" {print}'
A       changelogs/fragments/70887_galaxy_token.yml
A       test/integration/targets/unsafe_writes/aliases
A       test/integration/targets/unsafe_writes/basic.yml
[...]

The most recently added files are the ones at the top of the list.

Upvotes: 1

Related Questions