git 如何查看git存储库中单个文件的文件大小历史记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3150727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:22:23  来源:igfitidea点击:

How to see the file size history of a single file in a git repository?

git

提问by Echo says Reinstate Monica

Is there anyway to see how a file's size has changed through time in a git repository? I want to see how my main.js file (which is the combination of several files and minified) has grown and shrunk over time.

无论如何,是否可以在 git 存储库中查看文件大小随时间的变化?我想看看我的 main.js 文件(它是几个文件和缩小的组合)随着时间的推移是如何增长和缩小的。

采纳答案by igorw

You could create a script that uses the output from git show --pretty=raw <commit>to obtain the tree, then uses git ls-tree -r -lto obtain the blob you are looking for, including the file size.

您可以创建一个脚本,该脚本使用来自的输出git show --pretty=raw <commit>来获取树,然后使用git ls-tree -r -l来获取您要查找的 blob,包括文件大小。

In case you have ruby and the gritgem installed, here's a little script I threw together:

如果您安装了 ruby​​ 和gritgem,这是我拼凑的一个小脚本:

require 'grit'

if ARGV.size < 1
  puts 'usage: file-size FILE'
  puts 'run from within the git repo root'
  exit
end

filename = ARGV[0].to_s

repo = Grit::Repo.new('.')
commits = repo.log('master', filename)
commits.each do |commit|
  blob = commit.tree/filename
  puts "#{commit} #{blob.size} bytes"
end

Example usage (filename of script is file-size.rb), will show you the history for somedir/somefile:

示例用法(脚本的文件名是 file-size.rb),将显示 somedir/somefile 的历史记录:

myproject$ ruby file-size.rb somedir/somefile

回答by Jakub Nar?bski

You can use either git ls-tree -r -l <revision> <path>to get the blob size at given revision, e.g.

您可以使用任何一个git ls-tree -r -l <revision> <path>来获取给定修订版的 blob 大小,例如

$ git ls-tree -r -l v1.6.0 gitweb/README
100644 blob 825162a0b6dce8c354de67a30abfbad94d29fdde   16067    gitweb/README

The blob size in this example is '16067'. The disadvantage of this solution is that git ls-treecan process only one revision at once.

此示例中的 blob 大小为“16067”。这种解决方案的缺点是git ls-tree一次只能处理一个修订版。

You can use instead git cat-file --batch-check < <list-of-objects>instead, feeding it blob identifiers. If location of file didn't change through history (file was not moved), you can use git rev-list <starting-point> -- <path>to get list of revisions touching given path, translate them into names of blobs using <revision>:<path>extended SHA-1 syntax (see git-rev-parsemanpage), and feed it to git cat-file. Example:

您可以改为使用git cat-file --batch-check < <list-of-objects>,为它提供 blob 标识符。如果文件的位置没有通过历史记录更改(文件没有移动),您可以使用git rev-list <starting-point> -- <path>获取修改给定路径的列表,使用<revision>:<path>扩展的 SHA-1 语法将它们转换为 blob 的名称(请参阅git-rev-parse 手册页) ,并将其提供给git cat-file。例子:

$ git rev-list -5 v1.6.0 -- gitweb/README | 
  sed -e 's/$/:gitweb\/README/g' |
  git cat-file --batch-check
825162a0b6dce8c354de67a30abfbad94d29fdde blob 16067
6908036402ffe56c8b0cdcebdfb3dfacf84fb6f1 blob 16011
356ab7b327eb0df99c0773d68375e155dbcea0be blob 14248
8f7ea367bae72ea3ce25b10b968554f9b842fffe blob 13853
8dfe335f73c223fa0da8cd21db6227283adb95ba blob 13801

回答by jcsahnwaldt says GoFundMonica

Create a file called .gitattributesand add the following line:

创建一个名为的文件.gitattributes并添加以下行:

main.js -diff

This turns off line-based diffs for main.js. Now run the following command:

这将关闭基于行的差异main.js。现在运行以下命令:

git log --stat main.js

The log will include lines like

日志将包括类似的行

main.js | Bin 4316 -> 4360 bytes

After you're done, you should probably delete .gitattributes. I don't know what other changes in git's behavior may be caused by the -diffattribute.

完成后,您可能应该删除.gitattributes. 我不知道 git 行为的其他变化可能是由该-diff属性引起的。

Tested with git versions 1.7.12.4 and 1.7.9.5.

使用 git 版本 1.7.12.4 和 1.7.9.5 进行测试。

Source: ewall's answer and https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html#_marking_files_as_binary

来源:ewall 的回答和https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html#_marking_files_as_binary

回答by ocroquette

Here is a Bash function that will report the size over time in the following format.

这是一个 Bash 函数,它将以以下格式报告随时间变化的大小。

 LoC  Date                       Commit ID   Subject
 942  2019-08-31 18:09:34 +0200  35fc67c122  Declare some XML namespaces in replacement of OGCPrefixMapper, which has been removed from Apache SIS. https://issues.apache.org/jira/browse/SIS-126
 943  2019-08-09 16:52:29 +0200  e8438ab869  fix(GML): fix relative path resolving inside a jar
 934  2019-08-05 15:37:46 +0200  1e0c0b03c4  fix(GML): fix all test cases
 932  2019-07-30 15:54:53 +0200  fddea5db24  feat(GML): work on fallback for non-xsd Feature store
 932  2019-07-23 16:40:23 +0200  8d9a6a7dd0  feat(GML): improve support for custom XML mappings
 932  2019-06-26 15:18:43 +0200  43ea6e0bd7  feat(GML): add concurrency support for read/write operations
 932  2019-06-21 09:27:41 +0200  07a9993b4b  feat(GML): support group reference min/max occurs attributes
 932  2019-06-21 09:27:41 +0200  352a9104ae  feat(GML): fix resolving local files xsd paths
 919  2018-06-08 15:41:26 +0200  01ac7538e7  Merge branch 'master' into sis-migration
 919  2018-05-16 16:40:04 +0200  16fe7590c5  fix(JAXP): various fix for  WFS 2.0.0
 912  2018-04-11 10:09:22 +0200  bf3a38bdc4  chore(*): update JTS version 1.15.0
 912  2017-11-09 20:15:23 +0100  bc14dc4be1  fix(Client): fix minor problems on WFS querying
 901  2017-10-20 11:41:43 +0200  f686d7ff15  feat(Storage): add support for GML 2.1.2
 882  2017-05-16 23:07:31 +0200  f20c34c1e2  refactor(Feature): renamed the Geotk flavor of org.apache.sis.feature package as org.geotoolkit.feature.

Here is the function:

这是函数:

git-log-size() {
    git rev-list HEAD -- "" | while read cid; do
        git cat-file blob "$cid:" | wc -l | tr -d '\n'
        echo -n $'\t'
        git log -1 "--pretty=%ci%x09%h%x09%s" $cid
    done | column -t -s$'\t'
}

It is not particularly efficient, but does the job. It uses some utilities which are pretty common (wc, tr, column).

它不是特别有效,但可以完成工作。它使用了一些非常常见的实用程序(wc、tr、column)。

The size is reported as lines of code (LoC) since this is the common metric in software development, just change the "-l" option of wc if you prefer something else.

大小报告为代码行数 (LoC),因为这是软件开发中的常用指标,如果您更喜欢其他内容,只需更改 wc 的“-l”选项。

Here is how to call it:

这是如何调用它:

git-log-size modules/jaxp-xml-parser/geotk-jaxp-gml/src/main/java/org/geotoolkit/feature/xml/Utils.java

回答by user6225904

In case this is of use for someone, this script will show the size of a given file in different commits:

如果这对某人有用,此脚本将显示不同提交中给定文件的大小:

git log <file_name> | grep "^commit" | cut -f2 -d" " | while read hash; do
   echo -n "$hash -- "
   git show $hash:<file_path_off_of_git_root_without_leading_slash> | wc -c
done

回答by Maxim

On Windows I am using the following command:

在 Windows 上,我使用以下命令:

cmd /c "@echo off & for /l %N in (1 1 30) do git ls-tree -r -l HEAD~%N "C:\path\to\file.txt"

It will show size of each of latest 30 versions.

它将显示每个最新 30 个版本的大小。

If somebody can convert that to Linux command you are welcome... ))

如果有人可以将其转换为 Linux 命令,欢迎您...))

回答by ewall

While commands like git log <filename>, git whatchanged, etc. can show the history pertaining to that file, I don't see anywhere in either the built-in or custom pretty formats an option that shows size (sadly, the --log-sizeoption is only for the log messages!).

虽然像git log <filename>,git whatchanged等命令可以显示与该文件有关的历史记录,但我在内置或自定义漂亮格式中没有看到任何显示大小的--log-size选项(遗憾的是,该选项仅适用于日志消息!) .

However, you can get a rough idea of the size by seeing the total number of lines added and removed in each commit. You can sort of visualize it with the command git log --stat <filename>, which uses plus and minus signs. Or use git log --numstat <filename>to collect the number of lines added or removed in each commit and use the numbers in some other visualization.

但是,您可以通过查看每次提交中添加和删除的总行数来大致了解大小。您可以使用命令 将git log --stat <filename>其可视化,该命令使用加号和减号。或者用于git log --numstat <filename>收集每次提交中添加或删除的行数,并在其他一些可视化中使用这些数字。