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

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

问题描述

我有一个Git仓库,我想看看几个月前的文件。我在那天发现了修订版,它是: 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8 。我需要看看一个文件是什么样子,并将其保存到文件中。



我设法使用 gitk ,但它没有保存它的选项。我尝试过使用命令行工具,最接近的是:

  git-show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8 my_file.txt 
code>

但是,此命令显示diff而不是文件内容。我知道我以后可以使用类似于 PAGER = cat 的东西,并将输出重定向到一个文件,但我不知道如何获得实际的文件内容。



基本上,我正在寻找类似于 svn cat 的内容。

解决方案为了完成你自己的答案,语法的确是

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

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


  1. 分行名称(如建议

  2. HEAD + x number of ^ 字符

  3. 给定修订的SHA1散列

  4. 给定SHA1散列的前几个(可能是5个) / LI>

提示注意当使用 git show 始终指定一个来自存储库根目录的路径,而不是您当前的目录位置。

(虽然 Mike Morearty 提到,至少使用git 1.7.5.4,你可以在路径的开头加上 ./ 来指定一个相对路径 - 例如:

  git show HEAD ^^:./ test.py 

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



git ls-tree< rev>

显示一个或多个'blob'对象中的对象

$ g $ c $ git cat-file blob< file-SHA1>


因为它已经在特定版本中提交了一个文件(类似于svn
cat)。
使用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,并简单地将该对象转储到标准输出。






注意:自从Git 2.11(2016年第四季度)对 git cat-file 输出的内容过滤器!



请参阅 commit 3214594 commit 7bcf341 (2016年9月9日),提交7bcf341 (2016年9月9日) )和 commit b9e62f6 提交16dcc29 (2016年8月24日)作者: Johannes Schindelin( dscho

(由 Junio C Hamano - gitster
- 提交7889ed2 , 2016年9月21日)


cat-file :support - textconv / - 过滤器在批处理模式下



尽管 git hash-objects ,它是一个将on-filesystem数据流并将其放入Git对象存储的工具,允许执行outside-w orld-to-Git转换(例如,生产线末尾转换以及清理过滤器的应用程序),并且它在很早的时候就具有默认功能,其反向操作 git cat-file ,它从Git对象存储中获取一个对象并将其外部化外部世界,缺乏运行Git-to-outside-world的同等机制



  git config diff.txt.textconvtr A-Za-z N-ZA-Mn-za-m 
git cat-file --textconv --batch

注意: git cat-file --textconv 最近开始segfaulting(2017),其中
已在Git 2.15(2017年第4季度)中更正



请参阅提交cc0ea7c (2017年9月21日) Jeff King( peff
(合并通过 Junio C Hamano - gitster - commit bfbc2fc ,2017年9月28日)


I have a Git repository and I'd like to see how some file looked a few months ago. I found the revision at that date, it's: 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8. I need to see what did one file look like and also save that to a file.

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

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.

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

解决方案

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. 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

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.

(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

)


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

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

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 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.


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

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)

cat-file: support --textconv/--filters in batch mode

Even though "git hash-objects", which is a tool to take an on-filesystem data stream and put it into the Git object store, allowed to perform the "outside-world-to-Git" conversions (e.g. end-of-line conversions and application of the clean-filter), and it had the feature on by default from very early days, its reverse operation "git cat-file", which takes an object from the Git object store and externalize for the consumption by the outside world, lacked an equivalent mechanism to run the "Git-to-outside-world"

git config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <"
git cat-file --textconv --batch

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

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