我想使用 git 获取最近添加的文件列表。有什么合理的方法可以做到这一点吗?
答案1
git whatchanged --diff-filter=A
显示添加文件的提交以及它们添加的文件(最新文件优先)。
答案2
看@Dave Vandervies 的回答也一样。
A
1.仅查看已添加文件的日志:
git whatchanged --diff-filter=A
# [preferred] OR, very similar, but slightly cleaner/less verbose file information
git log --name-status --diff-filter=A
A
2. 查看添加和修改文件的日志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 whatchanged
vs 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.