首页 > 解决方案 > git - 显示更改文件的文件名和文件大小?

问题描述

有什么方法可以在差异上查看新文件大小?

如果我跑,git diff --name-only我会得到

path/to/changed/file.1
path/to/changed/file.2
path/to/changed/file.3

我想看到的是:

path/to/changed/file.1 1.7KB
path/to/changed/file.2 300Bytes
path/to/changed/file.3 9MB

我可以运行git diff --name-only | xargs ls -l,但这也给了我文件权限、日期等。

是否有内置git diff命令来显示文件大小?

标签: git

解决方案


这样:

git diff --name-only

你应该得到:

path/to/changed/file.1
path/to/changed/file.2
path/to/changed/file.3

由于此命令显示相对于项目根目录的更改文件,因此您必须将目录更改为项目的根目录例如,文件 .gitignore 所在的位置)。
现在您还可以通过运行以下命令获得每个更改文件的大小:

git diff --name-only  | xargs du -hs

示例输出为:

5.0K    path/to/changed/file.1
15.0K   path/to/changed/file.2
14.0K   path/to/changed/file.3

如果你想要你最喜欢的结果,运行这个:

git diff --name-only  | xargs du -hs | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }'

和输出:

path/to/changed/file.1  5.0K
path/to/changed/file.2  15.0K
path/to/changed/file.3  14.0K

推荐阅读