Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make "git status" show the file size of untracked files?

Tags:

git

It would be very handy for me to see the file size of untracked files. And maybe the old/new size of changed files.

Is it possible to configure git in a way to show it?

like image 410
Martin Thoma Avatar asked Feb 14 '19 12:02

Martin Thoma


People also ask

Does git status show untracked files?

If git status mentions "Untracked files:", you may need to add one or more untracked files. This status message is OK. We are up-to-date, there is nothing to commit, but there is still an untracked file.

What information can you find with git status?

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git. Status output does not show you any information regarding the committed project history.

How do I see file size in git?

You can use either git ls-tree -r -l <revision> <path> to get the blob size at given revision, e.g. The blob size in this example is '16067'.

How do I see all untracked files in git?

-unormal which shows untracked files and directories. This is the default behaviour of git status. -uall Which shows individual files in untracked directories.


2 Answers

git status --porcelain | awk '{print $2}' | xargs ls -hs | sort -h
  1. The git status --porcelain will show the file changed.
$ git status
?? IMG_20160813_205506_AO_HDR.jpg
?? IMG_20160813_205539_AO_HDR.jpg
?? IMG_20160813_211139_HDR.jpg
?? IMG_20160814_143649_HDR.jpg
  1. awk '{print $2}' will extract the content after ??
  2. Finally, the ls -hs will show the size of each file in a human readable format. and the sort -h will sort them by size.

Sample Output:

$ git status --porcelain | awk '{print $2}' | xargs ls -hs | sort -h
136 IMG_20160813_205506_AO_HDR.jpg
384 IMG_20160813_205539_AO_HDR.jpg
784 IMG_20160813_211139_HDR.jpg
5667898 IMG_20160814_143649_HDR.jpg
like image 51
ramwin Avatar answered Sep 28 '22 19:09

ramwin


No, you cannot make git status do that.

You may not need to make git status do that, because you can write your own command that does that instead. Use:

git -C "$(git rev-parse --show-cdup)" ls-files --other --exclude-standard

to obtain the file list. You can then use whatever command you like to view statistics about those files. You may want to run this command immediately after git status and have git status suppress its own listing with --untracked-files=no. For instance:

alias st='git status -uno;
  git -C "$(git rev-parse --show-cdup)" ls-files --others --exclude-standard -z | 
  xargs -0 ls -lR'

Here I've used -z as well since the command I am using, xargs -0 ls -l, can handle that, and expressed this as a shell alias rather than a Git alias.

There is a flaw here. While git status with -uall will enumerate all the untracked files within a directory, git ls-files --others won't: it behaves like a default git status, summarizing such files by printing only the containing directory name. The ls -l here will show the files within the directory; to stop that, use ls -ld instead, but of course you won't see any file sizes.

(To get modified files, use git ls-files -m rather than --others.)

like image 42
torek Avatar answered Sep 28 '22 18:09

torek