26

I want to get a list of the most recently added files using git. Is there any sane way to do this?

38

git whatchanged --diff-filter=A displays commits that added files, and the files they added, newest first.

2
  • 2
    git whatchanged is a bit of an anachronism, you can use git log --diff-filter=A --stat or git log --diff-filter=A --numstat --pretty='COMMIT: %H %cd' for something more machine readable. – Terry Brown Mar 21 '19 at 16:48
  • Thanks @TerryBrown. It looks like git officially recommends git log over git whatchanged as well. See the notes at the end of my answer I just added here: superuser.com/a/1636160/425838. – Gabriel Staples Mar 26 at 1:11
2
git diff --diff-filter=A --name-only HEAD

You could try this, --diff-filter=A is for files that are added and not committed yet. Refer diff manual page for more details. You could also try out ls-files as per your requirement. Hope this helps!

0

See @Dave Vandervies's answer as well.

1. See a log of only Added files:

git whatchanged --diff-filter=A

# [preferred] OR, very similar, but slightly cleaner/less verbose file information
git log --name-status --diff-filter=A

2. See a log of both Added AND Modified files:

If you want to see also modified files, use instead AM:

git whatchanged --diff-filter=AM

# [preferred] OR, very similar, but slightly cleaner/less verbose file information
git log --name-status --diff-filter=AM

3. To see a log of ANY and ALL types of changes, use the default:

git whatchanged

# [preferred] OR, very similar, but slightly cleaner/less verbose file information
git log --name-status

Other options include the following (from man git diff, search for --diff-filter=):

--diff-filter=[(A|C|D|M|R|T|U|X|B)...[*]]
    Select only files that are Added (A), Copied (C),
    Deleted (D), Modified (M), Renamed (R), have their
    type (i.e. regular file, symlink, submodule, ...)
    changed (T), are Unmerged (U), are Unknown (X), or
    have had their pairing Broken (B). Any combination
    of the filter characters (including none) can be
    used. When * (All-or-none) is added to the
    combination, all paths are selected if there is any
    file that matches other criteria in the comparison;
    if there is no file that matches other criteria,
    nothing is selected.
        
    Also, these upper-case letters can be downcased to
    exclude. E.g.  --diff-filter=ad excludes added and
    deleted paths.
        
    Note that not all diffs can feature all types. For
    instance, diffs from the index to the working tree
    can never have Added entries (because the set of
    paths included in the diff is limited by what is in
    the index). Similarly, copied and renamed entries
    cannot appear if detection for those types is
    disabled.

4. Optionally specify a branch name or commit hash:

Note that for all the commands above, you can optionally also specify a branch name or commit hash. Ex:

git whatchanged commit_hash

# [preferred] OR, very similar, but slightly cleaner/less verbose file information
git log --name-status commit_hash

5. To see just a single list (NOT a multi-entry log) of changed files, with their Modified/Added/etc. status:

git diff --name-status commit_hash

# OR, name only (no status character)
git diff --name-only commit_hash

NOTES: git whatchanged vs git log:

git officially recommends git log over git whatchanged. They say git whatchanged exists "primarily for historical reasons." See man git whatchanged:

DESCRIPTION
       Shows commit logs and diff output each commit introduces.

       New users are encouraged to use git-log(1) instead. The whatchanged
       command is essentially the same as git-log(1) but defaults to show
       the raw format diff output and to skip merges.

       The command is kept primarily for historical reasons; fingers of
       many people who learned Git long before git log was invented by
       reading Linux kernel mailing list are trained to type it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.