GIT底层命令之git cat-file

git-cat-file  - 提供资源库对象的内容或类型和大小信息

在GIT Pro中对该命令的介绍https://git-scm.com/docs/git-cat-file

概要

git cat-file (-t [--allow-unknown-type]| -s [--allow-unknown-type]| -e | -p | <type> | --textconv | --filters ) [--path=<path>] <object>
git cat-file (--batch | --batch-check) [ --textconv | --filters ] [--follow-symlinks]

在第一种形式中,该命令提供存储库中对象的内容或类型。除非使用-t或-p查找对象类型,或者使用-s查找对象大小,或者使用--textconv或--filters(暗示类型为“ blob”),否则<type>是必需的,type的值可以是 blob, tree, commit, tag。

例子如下:

我们查看本地仓库里的一个object文件:这个文件的SHA-1的值是08fe2720d8e3fe3a5f81fbb289bc4c7a522f13da,其前2位是文件夹名称,后38位是文件名称

我们直接通过 git cat-file 08fe2720d8e3fe3a5f81fbb289bc4c7a522f13da命令查看,是不行的,因为需要type指定文件类型

如果我们通过git cat-file blob 08fe2720d8e3fe3a5f81fbb289bc4c7a522f13da来查看文件内容,就可以看到文件内容是first line

我们通过git cat-file -p 08fe2720d8e3fe3a5f81fbb289bc4c7a522f13da方式也是可以的

yaosht@yaosht-PC MINGW64 ~/gitclient/.git/objects/08 (GIT_DIR!)
$ ll
total 1
-r--r--r-- 1 yaosht 197121 27  1月 18 12:33 fe2720d8e3fe3a5f81fbb289bc4c7a522f13da

yaosht@yaosht-PC MINGW64 ~/gitclient/.git/objects/08 (GIT_DIR!)
$ git cat-file 08fe2720d8e3fe3a5f81fbb289bc4c7a522f13da
usage: git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>
   or: git cat-file (--batch | --batch-check) [--follow-symlinks] [--textconv | --filters]

<type> can be one of: blob, tree, commit, tag
    -t                    show object type
    -s                    show object size
    -e                    exit with zero when there's no error
    -p                    pretty-print object's content
    --textconv            for blob objects, run textconv on object's content
    --filters             for blob objects, run filters on object's content
    --path <blob>         use a specific path for --textconv/--filters
    --allow-unknown-type  allow -s and -t to work with broken/corrupt objects
    --buffer              buffer --batch output
    --batch[=<format>]    show info and content of objects fed from the standard input
    --batch-check[=<format>]
                          show info about objects fed from the standard input
    --follow-symlinks     follow in-tree symlinks (used with --batch or --batch-check)
    --batch-all-objects   show all objects with --batch or --batch-check
    --unordered           do not order --batch-all-objects output


yaosht@yaosht-PC MINGW64 ~/gitclient/.git/objects/08 (GIT_DIR!)
$ git cat-file blob 08fe2720d8e3fe3a5f81fbb289bc4c7a522f13da
first line

yaosht@yaosht-PC MINGW64 ~/gitclient/.git/objects/08 (GIT_DIR!)
$ git cat-file -p 08fe2720d8e3fe3a5f81fbb289bc4c7a522f13da
first line

在第二种形式中,在stdin上提供了一个对象列表(由换行分隔),每个对象的SHA-1,类型和大小都打印在stdout上。 可以使用可选的<format>参数覆盖输出格式。 如果指定了--textconv或--filters,则输入需要列出对象名称,后跟路径名称,并用一个空格分隔,以便可以确定适当的驱动程序。(目前没有搞明白这个怎么用,等以后实际使用的时候再细细研究)

选项

<object> 要显示的对象的名称。有关拼写对象名称的更完整列表,请参阅 gitrevisions [7] 中的“指定修订”部分。

-t 显示由<object>标识的对象类型而不是内容。

-s 显示由<object>标识的对象大小,而不是内容。

-e 控制所有输出; 如果<object>存在并且是有效的对象,则返回零状态。

-p 基于其类型 Pretty-print(漂亮地打印)<object>的内容。

参考https://www.php.cn/manual/view/35095.html

https://git-scm.com/docs/git-cat-file

发布了212 篇原创文章 · 获赞 135 · 访问量 137万+

猜你喜欢

转载自blog.csdn.net/ystyaoshengting/article/details/104029248
今日推荐