如何获取在两次 Git 提交之间更改的所有文件的列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5096268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a list of all files that changed between two Git commits?
提问by Chen Kinnrot
Due to bureaucracy, I need to get a list of all changed files in my repository for a report (I started with existing source code).
由于官僚主义,我需要获取我的存储库中所有更改文件的列表以用于报告(我从现有源代码开始)。
What should I run to get this list?
我应该运行什么来获取此列表?
回答by Amber
For files changed between a given SHA and your current commit:
对于在给定 SHA 和当前提交之间更改的文件:
git diff --name-only <starting SHA> HEAD
or if you want to include changed-but-not-yet-committed files:
或者如果您想包含已更改但尚未提交的文件:
git diff --name-only <starting SHA>
More generally, the following syntax will always tell you which files changed between two commits (specified by their SHAs or other names):
更一般地说,以下语法将始终告诉您在两次提交之间更改了哪些文件(由它们的 SHA 或其他名称指定):
git diff --name-only <commit1> <commit2>
回答by Tim Bellis
To find the names of all files modified since your last commit:
要查找自上次提交以来修改的所有文件的名称:
git diff --name-only
Or (for a bit more information, including untracked files):
或者(有关更多信息,包括未跟踪的文件):
git status
回答by Flimm
To list all unstagedtracked changed files:
git diff --name-only
To list all stagedtracked changed files:
git diff --name-only --staged
To list all staged and unstagedtracked changed files:
{ git diff --name-only ; git diff --name-only --staged ; } | sort | uniq
To list all untrackedfiles (the ones listed by
git status
, so not including any ignored files):git ls-files --other --exclude-standard
要列出所有未暂存的跟踪更改文件:
git diff --name-only
要列出所有暂存跟踪更改的文件:
git diff --name-only --staged
要列出所有暂存和未暂存的跟踪更改文件:
{ git diff --name-only ; git diff --name-only --staged ; } | sort | uniq
列出所有未跟踪的文件(由 列出的文件
git status
,因此不包括任何被忽略的文件):git ls-files --other --exclude-standard
If you're using this in a shell script, and you want to programmatically check if these commands returned anything, you'll be interested in git diff
's --exit-code
option.
如果您在 shell 脚本中使用它,并且您想以编程方式检查这些命令是否返回任何内容,您将对git diff
's--exit-code
选项感兴趣。
回答by VonC
When I have added/modified/deleted many files (since the last commit), I like to look at those modifications in chronological order.
当我添加/修改/删除了许多文件时(自上次提交以来),我喜欢按时间顺序查看这些修改。
For that I use:
为此,我使用:
To list all non-staged files:
git ls-files --other --modified --exclude-standard
To get the last modified date for each file:
while read filename; do echo -n "$(stat -c%y -- $filename 2> /dev/null) "; echo $filename; done
列出所有非暂存文件:
git ls-files --other --modified --exclude-standard
获取每个文件的最后修改日期:
while read filename; do echo -n "$(stat -c%y -- $filename 2> /dev/null) "; echo $filename; done
Although ruvimsuggests in the comments:
xargs -0 stat -c '%y %n' --
To sort them from oldest to more recent:
sort
要将它们从最旧到最近排序:
sort
An alias makes it easier to use:
别名使其更易于使用:
alias gstlast='git ls-files --other --modified --exclude-standard|while read filename; do echo -n "$(stat -c%y -- $filename 2> /dev/null) "; echo $filename; done|sort'
Or (shorter and more efficient, thanks to ruvim)
或者(更短更高效,感谢ruvim)
alias gstlast='git ls-files --other --modified --exclude-standard|xargs -0 stat -c '%y %n' --|sort'
For example:
例如:
username@hostname:~> gstlast
2015-01-20 11:40:05.000000000 +0000 .cpl/params/libelf
2015-01-21 09:02:58.435823000 +0000 .cpl/params/glib
2015-01-21 09:07:32.744336000 +0000 .cpl/params/libsecret
2015-01-21 09:10:01.294778000 +0000 .cpl/_deps
2015-01-21 09:17:42.846372000 +0000 .cpl/params/npth
2015-01-21 12:12:19.002718000 +0000 sbin/git-rcd
I now can review my modifications, from oldest to more recent.
我现在可以查看我的修改,从最旧到最近。
回答by Fodder
I need a list of files that had changed content between two commits (only added or modified), so I used:
我需要一个在两次提交之间更改内容的文件列表(仅添加或修改),所以我使用了:
git diff --name-only --diff-filter=AM <commit hash #1> <commit hash #2>
The different diff-filter options from the git diff documentation:
来自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.
diff-filter=[(A|C|D|M|R|T|U|X|B)…?[*]]
仅选择已添加 (A)、已复制 (C)、已删除 (D)、已修改 (M)、已重命名 (R)、其类型(即常规文件、符号链接、子模块……?)已更改 (T) 的文件,未合并 (U)、未知 (X) 或配对已损坏 (B)。可以使用过滤器字符的任意组合(包括无)。当在组合中添加*(All-or-none)时,如果在比较中存在匹配其他条件的文件,则选择所有路径;如果没有符合其他条件的文件,则不选择任何内容。
此外,可以将这些大写字母小写以排除。例如 --diff-filter=ad 排除添加和删除的路径。
If you want to list the status as well (e.g. A / M), change --name-only
to --name-status
.
如果您还想列出状态(例如 A / M),请更改--name-only
为--name-status
。
回答by SuperNova
The list of unstaged modified can be obtained using git status
and the grep
command like below. Something like git status -s | grep M
:
可以使用git status
和grep
下面的命令获取未分段修改的列表。类似的东西git status -s | grep M
:
root@user-ubuntu:~/project-repo-directory# git status -s | grep '^ M'
M src/.../file1.js
M src/.../file2.js
M src/.../file3.js
....
回答by Kurapika
With git show
you can get a similar result. For look the commit (like it looks on git log
view) with the list of files included in, use:
有了git show
你可以得到类似的结果。要查看git log
包含文件列表的提交(就像在视图中看起来一样),请使用:
git show --name-only [commit-id_A]^..[commit-id_B]
Where [commit-id_A]
is the initial commit and [commit-id_B]
is the last commit than you want to show.
[commit-id_A]
初始提交在哪里,以及[commit-id_B]
您想要显示的最后一次提交在哪里。
Special attentionwith ^
symbol. If you don't put that, the commit-id_A information will not deploy.
特别注意与^
符号。如果你不放那个,commit-id_A 信息将不会部署。
回答by Saurabh Pal
If you want to check the changed files you need to take care of many small things like which will be best to use , like if you want to check which of the files changed just type
如果您想检查更改的文件,您需要处理许多小事情,例如最好使用哪些文件,例如如果您想检查哪些文件更改了只需键入
git status -- it will show the files with changes
git status -- 它将显示有更改的文件
then if you want to know what changes are to be made it can be checked in ways ,
那么如果您想知道要进行哪些更改,可以通过以下方式进行检查,
git diff -- will show all the changes in all files
git diff -- 将显示所有文件中的所有更改
it is good only when only one file is modified
只有修改一个文件才好
and if you want to check particular file then use
如果你想检查特定文件然后使用
git diff
混帐差异