由于官僚主义的原因,我需要为报告获得存储库中所有更改的文件的列表(我从现有源代码开始)。

我该怎么跑才能拿到这个列表?


当前回答

我需要在两次提交之间更改内容的文件列表(只添加,a,或修改,M),所以我使用:

git diff --name-only --diff-filter=AM <from_commit_hash> <to_commit_hash>

下面是git diff文档中不同的——diff-filter选项(参见man git diff):

--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.

如果你也想列出状态(例如A / M),将——name-only更改为——name-status:

git diff --name-status --diff-filter=AM <from_commit_hash> <to_commit_hash>

其他回答

我需要在两次提交之间更改内容的文件列表(只添加,a,或修改,M),所以我使用:

git diff --name-only --diff-filter=AM <from_commit_hash> <to_commit_hash>

下面是git diff文档中不同的——diff-filter选项(参见man git diff):

--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.

如果你也想列出状态(例如A / M),将——name-only更改为——name-status:

git diff --name-status --diff-filter=AM <from_commit_hash> <to_commit_hash>

列出所有未执行跟踪的更改文件: Git diff -name-only 使用实例列出所有被跟踪的已更改文件。 Git diff -name-only - staging 列出所有已跟踪和未跟踪的已更改文件: {git diff——name-only;Git diff -name-only - staging;} | sort | uniq 列出所有未跟踪的文件(由git状态列出的文件,因此不包括任何被忽略的文件): Git ls-files——other——exclude-standard

如果你在shell脚本中使用它,并且你想通过编程方式检查这些命令是否返回了任何东西,你会对git diff的——exit-code选项感兴趣。

一些过滤器是为我工作在GitHub动作取决于类型(pull_request或合并为主):

git --no-pager diff --name-only --diff-filter=ACMRT ${{github.event.pull_request.base.sha}} ${{ github.event.pull_request.head.sha }}
git --no-pager diff --name-only --diff-filter=ACMRT ${{github.event.pull_request.base.sha}} ${{github.sha}}
git log -m -1 --name-only --pretty="format:" ${{ github.sha }}

试试这些,看看哪一个适合你的需要。

查找自上次提交以来修改过的所有文件的名称:

git diff --name-only

或者(获取更多信息,包括未跟踪的文件):

git status

获取修改文件列表并将其保存到某个文本文件的最简单方法是:

git diff --name-only HEAD^ > modified_files.txt