Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Get file size of a git commit without a tmp file

I've answered this question, suggesting using git hooks, but frankly, I'M not fully satisfied with the second part, since creating a temporary file just to get the size of the commit looks redundant and ugly. Is there a way to get the size directly? Assume the standard git implementation.

As suggested by @Cyrus, git diff --cached | wc -c is a good approximation, but it's encoding dependent.

I also tried process substitution ls -l <(git diff --cached) | awk '{print $5}' but that returns 0.

Edit: Based on @torek's answer I came up with these two solutions:

git ls-files --stage | cut -d' ' -f2 | xargs -L 1 git cat-file -s | awk '{s+=$1} END {print s}'

git diff --cached | awk -F. '/index/ {print $3}' | xargs -L 1 git cat-file -s | awk '{s+=$1} END {print s}'

The latter also handles new files.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

To get the size (in bytes) of a git object, use git cat-file -s revspec. For instance:

$ git cat-file -s HEAD:Makefile
950

Beware that this will show you the object size of a tree:

$ git cat-file -t HEAD:
tree
$ git cat-file -s HEAD:
546

For a large collection of IDs you might want to use git cat-file --batch-check (which, since git 1.8.4, now takes a format argument to select what information to print; see the documentation).

over 4 years ago · Santiago Trujillo Report

0

I think that git diff --cached --numstat is basically the main source of the information. For me it's not completely clear what restrictions you'd like to put on a commit but to summarize number of insertions and deletions you could use something like this:

git diff --numstat --cached | awk '
   BEGIN { insertions=0; deletions=0; }
   { insertions+=$1; deletions+=$2; }
   END { printf("inserttions=%d, deletions=%d\n", insertions, deletions); }'
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!