使用 git 查找最近添加的文件

使用 git 查找最近添加的文件

我想使用 git 获取最近添加的文件列表。有什么合理的方法可以做到这一点吗?

答案1

git whatchanged --diff-filter=A显示添加文件的提交以及它们添加的文件(最新文件优先)。

答案2

@Dave Vandervies 的回答也一样。

A1.仅查看已添加文件的日志:

git whatchanged --diff-filter=A

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

A2. 查看添加和修改文件的日志M

如果您还想查看已修改的文件,请使用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. 要查看任何和所有类型更改的日志,请使用默认值:

git whatchanged

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

其他选项包括以下内容(来自man git diff,搜索--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. 可选择指定分支名称或提交哈希:

请注意,对于上述所有命令,您还可以选择指定分支名称或提交哈希。例如:

git whatchanged commit_hash

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

5. 要查看已更改文件的单个列表(而非多条目日志),以及它们的M修改/A添加/等状态:

git diff --name-status commit_hash

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

注释:git whatchangedvs git log

git官方建议git log超过git whatchanged。他们说git whatchanged存在“主要是由于历史原因”。参见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.

答案3

git diff --diff-filter=A --name-only HEAD

您可以尝试这个,--diff-filter=A适用于已添加但尚未提交的文件。请参阅差异手册页了解更多详情。您也可以尝试ls 文件根据您的要求。希望这能有所帮助!

相关内容