简体   繁体   English

在 git 中查找未提交或未跟踪文件的总大小

[英]Find total size of uncommitted or untracked files in git

I have a big horrible pile of code and I am setting it up in version control.我有一大堆可怕的代码,我正在版本控制中设置它。

I would like a command I can run on Linux to give me the total size of the files that would be committed and pushed if I ran git add -A && git commit -am 'initial commit'如果我运行git add -A && git commit -am 'initial commit' ,我想要一个可以在 Linux 上运行的命令,以提供将提交和推送的文件的总大小

The total size is needed, also a break down by folder would be handy.需要总大小,按文件夹细分也很方便。

I will then use this to build up my ignores so that I can get the repo to a realistic size before I push it up然后,我将使用它来建立我的忽略,以便在我将它推高之前将回购设置为实际大小

I think I have answered my own question:我想我已经回答了我自己的问题:

for f in `git status --porcelain | sed 's#^...##'`; do du -cs $f | head -n 1; done | sort -nr;  echo "TOTAL:"; du -cs .

However I'm open to any better ideas or useful tricks.但是,我愿意接受任何更好的想法或有用的技巧。 My current output is 13GB :)我当前的输出是 13GB :)


The above command is basically there, it gives me the total line by line from git status but doesn't give me the total sum.上面的命令基本上就在那里,它从 git status 逐行给我总和,但没有给我总和。 I'm currently getting the total of all files at the end which is not correct.我目前正在最后得到所有文件的总数,这是不正确的。 I tried some use of bc but couldn't get it to work我尝试了一些使用bc但无法让它工作

I adapted the answer of edmondscommerce by adding a simple awk statement which sums the output of the for loop and prints the sum (divided by 1024*1024 to convert to Mb)我通过添加一个简单的 awk 语句来调整 edmondscommerce 的答案,该语句将 for 循环的输出相加并打印总和(除以 1024*1024 以转换为 Mb)

for f in `git status --porcelain | sed 's#^...##'`; do du -cs $f | head -n 1; done | sort -nr  | awk ' {tot = tot+$1; print } END{ printf("%.2fMb\n",tot/(1024*1024)) }' 

Note that --porcelain prints pathnames relative to the root of the git repos.请注意, --porcelain 打印相对于 git repos 根目录的路径名。 So, if you do this in a subdirectory the du statement will not be able to find the files..因此,如果您在子目录中执行此操作,du 语句将无法找到文件。

(whoppa; my first answer in SoF, may the force be with it) (whoppa;我在 SoF 中的第一个答案,愿力量与它同在)

I've used a modified version of this, because I had files with spaces in them which made it crash.我使用了这个的修改版本,因为我的文件中有空格,导致它崩溃。 I was also unsure about the size calculations and removed a useless head :我也不确定尺寸计算并删除了一个无用的head

git status --porcelain | sed 's/^...//;s/^"//;s/"$//' | while read path; do
    du -bs "$path" ;
done | sort -n | awk ' {tot = tot+$1; print } END { printf("%.2fMB\n",tot/(1024*1024)) }'

I prefer to use while as it's slightly safer than for : it can still do nasty things with files that have newlines in them so I wish there was a to pass null -separate files yet still be able to grep for the status, but I couldn't find a nice way for that.我更喜欢使用while ,因为它比for稍微安全一些:它仍然可以对带有换行符的文件做一些讨厌的事情,所以我希望有一个传递null -separate 文件但仍然能够 grep 获取状态,但我不能找不到一个好的方法。

Since version 2.11 , git provides a handy "count-objects" command :2.11 版本开始,git 提供了一个方便的“count-objects”命令:

git count-objects -H

If this is not enough, I would recommend git-sizer from github : https://github.com/github/git-sizer如果这还不够,我会推荐来自 github 的 git-sizer: https ://github.com/github/git-sizer

git-sizer --verbose

Detailed usage here : https://github.com/github/git-sizer/#usage详细用法在这里: https ://github.com/github/git-sizer/#usage

Since you're just adding everything, I don't see any reason to go via Git.由于您只是添加所有内容,因此我认为没有任何理由通过 Git。 Just use the ordinary Unix tools: du , find , &c.只需使用普通的 Unix 工具: dufind和 &c。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM