使用git show
为了完成你自己的答案,语法确实是
git show object
git show $REV:$FILE
git show somebranch:from/the/root/myfile.txt
git show HEAD^^^:test/test.py
该命令采用通常的修订样式,这意味着您可以使用以下任何一种:
- 分支名称(suggested by ash)
-
HEAD + x 个^ 字符
- 给定修订版的 SHA1 哈希
- 给定 SHA1 哈希的前几个(可能是 5 个)字符
提示请务必记住,在使用“git show”时,始终指定从存储库根目录开始的路径,而不是您当前的目录位置。 p>
(虽然Mike Morearty 提到,至少在 git 1.7.5.4 中,您可以通过将“./”放在路径开头来指定相对路径。例如:
git show HEAD^^:./test.py
)
使用git restore
使用 Git 2.23+(2019 年 8 月),您还可以使用 git restore 替换 the confusing git checkout command
git restore -s <SHA1> -- afile
git restore -s somebranch -- afile
这将仅在工作树上恢复 “源”(-s) 提交 SHA1 或分支 somebranch 中存在的文件。
要恢复索引:
git restore -s <SHA1> -SW -- afile
(-SW:--staged --worktree的缩写)
如the comments starwarswii 所述
它允许您将内容通过管道传输到文件中,如果您只想快速比较来自提交的文件,这非常有用。
例如你可以这样做:
git show 1234:path/to/file.txt > new.txt
git show 1234~:path/to/file.txt > old.txt
然后比较它们。
使用低级 git 管道命令
在 git1.5.x 之前,这是通过一些管道完成的:
git ls-tree <rev>
在提交中显示一个或多个“blob”对象的列表
git cat-file blob <file-SHA1>
cat 文件,因为它已在特定版本中提交(类似于 svn
猫)。
使用git ls-tree 检索给定文件sha1 的值
git cat-file -p $(git-ls-tree $REV $file | cut -d " " -f 3 | cut -f 1)::
git-ls-tree 列出了修订版$REV 中$file 的对象ID,它被从输出中截取并用作git-cat-file 的参数,它应该真正称为git-cat-object,并简单地转储它反对stdout。
注意:从 Git 2.11(2016 年第四季度)开始,您可以将内容过滤器应用于 git cat-file 输出。
看
commit 3214594,
commit 7bcf341(2016 年 9 月 9 日),
commit 7bcf341(2016 年 9 月 9 日)和
commit b9e62f6,
commit 16dcc29(2016 年 8 月 24 日)Johannes Schindelin (dscho)。
(由 Junio C Hamano -- gitster -- 在 commit 7889ed2 中合并,2016 年 9 月 21 日)
git config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <"
git cat-file --textconv --batch
注意:“git cat-file --textconv”最近(2017 年)开始出现段错误,已在 Git 2.15(2017 年第四季度)中更正
参见Jeff King (peff) 的commit cc0ea7c(2017 年 9 月 21 日)。
(由 Junio C Hamano -- gitster -- 合并于 commit bfbc2fc,2017 年 9 月 28 日)