git count files in the staged index

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 status to do this.

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


Solution 1:

If you want something a script can use:

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

If you want something human readable:

git diff --cached --stat

Solution 2:

This worked for me:

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

it returns a number

Solution 3:

For what it's worth, I prefer:

git diff --stat | tail -n1

Outputs something like:

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