Using git show
为了完成你自己的答案,语法确实是
git show object
git show $REV:$FILE
git show somebranch:from/the/root/myfile.txt
git show HEAD^^^:test/test.py
该命令采用通常的修订风格,这意味着您可以使用以下任意命令:
- 分支名称(如建议 https://stackoverflow.com/review/suggested-edits/3041393 by ash https://stackoverflow.com/users/17708/ash)
-
HEAD
+ x 数量^
人物
- 给定修订版的 SHA1 哈希值
- 给定 SHA1 哈希值的前几个(可能是 5)个字符
Tip重要的是要记住,当使用“git show
", 始终指定存储库根目录的路径,不是您当前的目录位置。
(虽然迈克·莫尔蒂 https://stackoverflow.com/users/179675/mike-morearty提到,至少在 git 1.7.5.4 中,您可以通过输入“来指定相对路径”./
" 在路径的开头。例如:
git show HEAD^^:./test.py
)
Using git restore
使用 Git 2.23+(2019 年 8 月),您还可以使用git restore https://git-scm.com/docs/git-restore它取代了令人困惑的git checkout command https://stackoverflow.com/a/57066202/6309
git restore -s <SHA1> -- afile
git restore -s somebranch -- afile
这将仅在工作树上恢复存在于“来源” (-s
)提交 SHA1 或分支somebranch
.
还要恢复索引:
git restore -s <SHA1> -SW -- afile
(-SW
: 短缺--staged --worktree
)
如中所述评论 https://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file-from-a-specific-revision-in-git/610315#comment120557219_610315 by 星球大战 https://stackoverflow.com/users/3249197/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
列出对象 ID$file
修订中$REV
,这被从输出中删除并用作参数git-cat-file
,这实际上应该被称为git-cat-object
,然后简单地将该对象转储到stdout
.
注意:从 Git 2.11(2016 年第 4 季度)开始,您可以将内容过滤器应用于git cat-file
output.
See
commit 3214594 https://github.com/git/git/commit/321459439e19517c412cab1cfbb64a2749f272c9,
commit 7bcf341 https://github.com/git/git/commit/7bcf341453572a227036afec76bdab451b798870 (09 Sep 2016),
commit 7bcf341 https://github.com/git/git/commit/7bcf341453572a227036afec76bdab451b798870 (09 Sep 2016), and
commit b9e62f6 https://github.com/git/git/commit/b9e62f60115c75c5be5de593862925c8b8d7e683,
commit 16dcc29 https://github.com/git/git/commit/16dcc2992b80d30f99c41fd7cc858e9d1c9dbca3 (24 Aug 2016) by Johannes Schindelin (dscho) https://github.com/dscho.
(Merged by Junio C Hamano -- gitster -- https://github.com/gitster in commit 7889ed2 https://github.com/git/git/commit/7889ed25ac709624446b0b4d887dd3633e81f44a, 21 Sep 2016)
git config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <"
git cat-file --textconv --batch
Note: "git cat-file --textconv
最近(2017 年)开始出现段错误,该问题已在 Git 2.15(2017 年第 4 季度)中得到纠正
See commit cc0ea7c https://github.com/git/git/commit/cc0ea7c9e5c3d3bf9623415737b595c9d26b9b6d (21 Sep 2017) by Jeff King (peff) https://github.com/peff.
(Merged by Junio C Hamano -- gitster -- https://github.com/gitster in commit bfbc2fc https://github.com/git/git/commit/bfbc2fccfdff0fe189337de67e16fc132b8ee2a5, 28 Sep 2017)