有没有一种方法只列出已更改的目录?
比如说我在git根目录中,~/project
我更改的文件是
~/project/subtool/file1
~/project/subtool/file2
~/project/subtool3/file1
我只想要
~/project/subtool
~/project/subtool3
有没有一种方法只列出已更改的目录?
比如说我在git根目录中,~/project
我更改的文件是
~/project/subtool/file1
~/project/subtool/file2
~/project/subtool3/file1
我只想要
~/project/subtool
~/project/subtool3
您可以使用git-diff
命令,并带上--dirstat
参数。
在您的场景中,假设您有以下提交记录:
$ git diff --name-status HEAD~1
M subtool/file1
M subtool/file2
M subtool3/file1
$ git diff --dirstat=files,0 HEAD~1
66.6% subtool/
33.3% subtool3/
请确保添加,0
,否则git diff
默认仅显示至少有3%更改的目录。我也选择了文件
,因为这是计算成本最小的选项,而且您似乎也不关心具体的更改。
如果您能够使用sed
,则可以摆脱百分比值(您可能需要微调正则表达式以适应您的需求):
$ git diff --dirstat=files,0 HEAD~1 | sed 's/^[ 0-9.]\+% //g'
subtool/
subtool3/
git diff-tree
就是为此而构建的。doit() { git diff-tree "$@" | awk '$1$2~/04/' | cut -f2-; }
- jthillsed
命令中添加 -E
标志,并从 \+
中移除正斜杠。这是我使用的命令:sed -E 's/^[ 0-9.]+% //g'
。 - Doug Ayerssed
命令中,您可以删除所有子文件夹名称,以便只剩下顶层文件夹。git diff --dirstat=files,0 HEAD~1 | sed -E 's/^[ 0-9.]+% //g' | sed -E 's/\/.*$//g'
- Doug Ayersgit diff --name-only HEAD~1 | awk -F "/*[^/]*/*$" '{ print ($1 == "" ? "." : $1); }' | sort | uniq
根目录的变更将会被标记为.
git diff --dirstat=files,0 HEAD~1
的限制在于它无法展示根目录的变更。
这里提供一个替代方案,使用xargs
和dirname
而不是像sed
或awk
那样的工具。
git diff HEAD~1 --name-only | xargs dirname | sort | uniq
git diff …
将列出修改的文件路径。| xargs dirname
将文件路径转换为目录路径。| sort | uniq
将删除重复项。请注意,如果您修改了存储库根目录中的任何文件,则会生成一行.
。
在采用@Lari Hotari的建议中添加split和uniq选项后,可以获取已修改的唯一目录。
$(git diff --name-only HEAD~1 | awk -F "/*[^/]*/*$" '{ print ($1 == "" ? "." : $1); }' | cut -d "/" -f 1 | sort | uniq)
此命令将监视monorepo中子项目的所有更改,并可以使用 npm i -ws
命令仅为更改的工作区安装依赖项
git diff --name-only | cut -d/ -f1 | uniq
还会给出更改模块的根目录。 - The Foolgit diff --dirstat HEAD~100..HEAD