git count files in the staged index

前端 未结 8 503
难免孤独
难免孤独 2021-02-01 12:05

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

相关标签:
8条回答
  • 2021-02-01 12:35

    This worked for me:

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

    it returns a number

    0 讨论(0)
  • 2021-02-01 12:35

    Try git status -s:

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

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

    0 讨论(0)
  • 2021-02-01 12:37

    This has lots of answers... but the best command imo (it doesn't require any piping and is a pure native git command) is just the following. Note that this counts deleted, modified, and added files:

    git diff --cached --shortstat
    

    The output is only one line:

    X files changed, Y insertions(+), Z deletions(-)
    

    If no changes have been made, it prints nothing (not even a new empty line).

    It's also obvious how to get the same result for unstaged changes (just omit the --cached flag):

    git diff --shortstat
    
    0 讨论(0)
  • 2021-02-01 12:53

    For what it's worth, I prefer:

    git diff --stat | tail -n1
    

    Outputs something like:

    10 files changed, 74 insertions(+), 123 deletions(-)
    
    0 讨论(0)
  • 2021-02-01 12:57

    For anyone looking for a PowerShell solution:

    (git diff --cached --numstat | Measure-Object -Line).Lines
    
    0 讨论(0)
  • 2021-02-01 12:59

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

    git ls-files --cached | wc -l
    
    0 讨论(0)
提交回复
热议问题