git count 暂存索引中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3162444/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:23:10  来源:igfitidea点击:

git count files in the staged index

git

提问by Bradley

I'm trying to figure out how to easily count the files in my uncommitted index.

我试图弄清楚如何轻松计算未提交索引中的文件。

I've tried:

我试过了:

git status | grep '#' | wc -l

but there are a few lines that start with #that don't represent changed files. Anyone got anything better? Figured there had to be a flag for git statusto do this.

但有几行开头#不代表更改的文件。有人有更好的吗?认为必须有一面旗帜git status才能做到这一点。

Even tools like GitX don't easily allow you to select the staged files/directories and see how many of them there are.

即使像 GitX 这样的工具也不能轻易让您选择暂存文件/目录并查看其中有多少。

回答by mkarasek

If you want something a script can use:

如果你想要一些脚本可以使用的东西:

git diff --cached --numstat | wc -l

git diff --cached --numstat | wc -l

If you want something human readable:

如果你想要一些人类可读的东西:

git diff --cached --stat

git diff --cached --stat

回答by chrisjlee

This worked for me:

这对我有用:

git status | grep 'modified:' | wc -l

git status | grep 'modified:' | wc -l

it returns a number

它返回一个数字

回答by fwiw

For what it's worth, I prefer:

对于它的价值,我更喜欢:

git diff --stat | tail -n1

Outputs something like:

输出类似:

10 files changed, 74 insertions(+), 123 deletions(-)

回答by tobiw

Try git status -s:

尝试git status -s

git status -s | egrep "^M" | wc -l

Mdirectly after start-of-line (^) indicates a staged file. ^ M, with a space, would be an unstaged but changed file.

M紧接在行首 ( ^) 之后表示一个暂存文件。^ M, 带有空格,将是未暂存但已更改的文件。

回答by Jean-Bernard Jansen

Maybe it was not available 9 years ago, but as of 2019, ls-filesis much faster than diff --stat:

也许它在 9 年前不可用,但截至 2019 年,ls-files它比diff --stat

git ls-files --cached | wc -l

回答by Mahender

Below should cover for all cases (new, modified, deleted and even untracked), it returns a number.

下面应该涵盖所有情况(新的、修改的、删除的甚至未跟踪的),它返回一个数字。

(git status -s |select-string -Pattern "^\s*[A|\?|D|M]" -AllMatches).Matches.Count

(git status -s |select-string -Pattern "^\s*[A|\?|D|M]" -AllMatches).Matches.Count