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

查看:84
本文介绍了如何从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.

推荐答案

使用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. 分支名称(如
  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月)中,您还可以使用 git restore ,它替换了令人困惑的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的缩写)

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

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

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

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

git cat-file blob <file-SHA1>
在特定修订版中提交已提交的文件(类似于svn 猫). 使用git ls-tree检索给定文件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,该对象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日), 提交7bcf341 (2016年9月9日),以及 提交b9e62f6 提交16dcc29 (2016年8月24日)由 Junio C Hamano-gitster-合并在

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)

请参见提交cc0ea7c (2017年9月21日): //github.com/peff"rel =" noreferrer>杰夫·金(peff).
(由 Junio C Hamano-gitster-合并在

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天全站免登陆