Home > OS >  How can I calculate the number of lines changed since last commit in Git?
How can I calculate the number of lines changed since last commit in Git?

Time:05-11

Is there an easy way to calculate number of lines changed in tracked files since last commit? This is about monitoring changes that are not commited yet. Context is that I would like to write a tool that will warn me when I modify large portion of code and haven't commited these changes (facilitate atomic commits). I know I can do git status or git status --short and extract modified (and not tracked) files. Then I can use git diff --stat <file> and loop over files to extract number of changed lines. Is there any one- or few-liner solution that could do that in bash/zsh or python?

CodePudding user response:

If you just want to calculate the LOC difference then you can run this command to get the LOCs (reference SO Answer: https://stackoverflow.com/a/4822516/970422)

git ls-files | xargs cat | wc -l

You can run this command on the parent branch and in your local copy to get the difference in LOCs.

CodePudding user response:

git diff --stat | tail --lines=1 (or -n1 in BSD land) should give you what you want, if I understand correctly:

6 files changed, 726 insertions( ), 6 deletions(-)

  • Related