如何从 Git 中的特定修订版检索单个文件? [英] How to retrieve a single file from a specific revision in Git?

查看:13
本文介绍了如何从 Git 中的特定修订版检索单个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Git 存储库,我想看看几个月前一些文件的样子.我在那个日期找到了修订版;它是 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8.我需要查看一个文件的外观,并将其另存为(新")文件.

I have a Git repository and I'd like to see how some files looked a few months ago. I found the revision at that date; it's 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8. I need to see what one file looks like, and also save it as a ("new") file.

我设法使用 gitk 看到了该文件,但它没有保存它的选项.我尝试使用命令行工具,最接近的是:

I managed to see the file using gitk, but it doesn't have an option to save it. I tried with command-line tools, the closest I got was:

git-show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8 my_file.txt

但是,此命令显示的是差异,而不是文件内容.我知道我以后可以使用诸如 PAGER=cat 之类的东西并将输出重定向到文件,但我不知道如何获取实际的文件内容.

However, this command shows a diff, and not the file contents. I know I can later use something like PAGER=cat and redirect output to a file, but I don't know how to get to the actual file content.

基本上,我正在寻找类似 svn cat 的东西.

Basically, I'm looking for something like svn cat.

推荐答案

Using git show

要完成你自己的答案,语法确实是

Using git show

To complete your own answer, the syntax is indeed

git show object
git show $REV:$FILE
git show somebranch:from/the/root/myfile.txt
git show HEAD^^^:test/test.py

该命令采用通常的修订风格,这意味着您可以使用以下任何一种:

The command takes the usual style of revision, meaning you can use any of the following:

  1. 分支名称(如建议ash)
  2. HEAD + x 个 ^ 字符数
  3. 给定修订的 SHA1 哈希值
  4. 给定 SHA1 哈希的前几个(可能是 5 个)字符
  1. branch name (as suggested by ash)
  2. HEAD + x number of ^ characters
  3. The SHA1 hash of a given revision
  4. The first few (maybe 5) characters of a given SHA1 hash

提示请务必记住,在使用git show"时,始终指定从存储库根目录开始的路径,不是您当前的目录位置.

Tip It's important to remember that when using "git show", always specify a path from the root of the repository, not your current directory position.

(尽管 Mike Morearty 提到,至少在 git 1.7.5.4 中,您可以指定相对路径通过将./"放在路径的开头.例如:

(Although Mike Morearty mentions that, at least with git 1.7.5.4, you can specify a relative path by putting "./" at the beginning of the path. For example:

git show HEAD^^:./test.py

)

使用 Git 2.23+(2019 年 8 月),您还可以使用 gitrestore 替换了 令人困惑的 git checkout 命令

With Git 2.23+ (August 2019), you can also use git restore which replaces the confusing git checkout command

git restore -s <SHA1>     -- afile
git restore -s somebranch -- afile

这将在工作树上仅恢复源"中存在的文件.(-s) 提交 SHA1 或分支 somebranch.
还要恢复索引:

That would restore on the working tree only the file as present in the "source" (-s) commit SHA1 or branch somebranch.
To restore also the index:

git restore -s <SHA1> -SW -- afile

(-SW:--staged --worktree的缩写)

评论 来自 starwarswii

它允许您将内容通过管道传输到文件中,如果您只想快速比较提交中的文件,这非常有用.

It lets you pipe the contents into a file, which is great if you want to just quickly compare files from a commit.

例如你可以这样做:

git show 1234:path/to/file.txt > new.txt 
git show 1234~:path/to/file.txt > old.txt

然后比较它们.


使用低级 git 管道命令

在 git1.5.x 之前,这是通过一些管道完成的:


Using low-level git plumbing commands

Before git1.5.x, this was done with some plumbing:

git ls-tree
在提交中显示一个或多个blob"对象的列表

git ls-tree <rev>
show a list of one or more 'blob' objects within a commit

git cat-file blob
cat 文件,因为它已在特定修订版中提交(类似于 svn猫).使用 git ls-tree 检索给定 file-sha1 的值

git cat-file blob <file-SHA1>
cat a file as it has been committed within a specific revision (similar to svn cat). use git ls-tree to retrieve the value of a given file-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-ls-tree lists the object ID for $file in revision $REV, this is cut out of the output and used as an argument to git-cat-file, which should really be called git-cat-object, and simply dumps that object to stdout.

注意:从 Git 2.11(2016 年第四季度)开始,您可以对 git cat-file 输出应用内容过滤器.

Note: since Git 2.11 (Q4 2016), you can apply a content filter to the git cat-file output.

提交 3214594提交 7bcf341(2016 年 9 月 9 日),commit 7bcf341(2016 年 9 月 9 日),以及提交 b9e62f6commit 16dcc29(2016 年 8 月 24 日)来自 约翰内斯·辛德林 (dscho).
(由 Junio C Hamano 合并 -- gitster --commit 7889ed2,2016 年 9 月 21 日)

See commit 3214594, commit 7bcf341 (09 Sep 2016), commit 7bcf341 (09 Sep 2016), and commit b9e62f6, commit 16dcc29 (24 Aug 2016) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 7889ed2, 21 Sep 2016)

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 年第四季度)中得到纠正

Note: "git cat-file --textconv" started segfaulting recently (2017), which has been corrected in Git 2.15 (Q4 2017)

参见 commit cc0ea7c(2017 年 9 月 21 日)杰夫·金 (peff).
(由 Junio C Hamano 合并 -- gitster --提交 bfbc2fc,2017 年 9 月 28 日)

See commit cc0ea7c (21 Sep 2017) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit bfbc2fc, 28 Sep 2017)

这篇关于如何从 Git 中的特定修订版检索单个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆