git-checkout使用新名称的旧文件修订版 [英] git-checkout older revision of a file under a new name

查看:100
本文介绍了git-checkout使用新名称的旧文件修订版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件 main.cpp 在我的编辑器中打开。



我想查看之前在编辑器中也修改过 main.cpp



现在我这样做的方式就是这样。

 在编辑器中关闭main.cpp

prompt> mv main.cpp tmp
prompt> git checkout HEAD ^ main.cpp
prompt> mv main.cpp old_main.cpp
prompt> mv tmp main.cpp
提示>

在编辑器中打开main.cpp和old_main.cpp

它可以被简化,所以我不必在编辑器中关闭main.cpp?



我所希望的是可以做到这一点的 git-checkout 的变体。






更新:im在mac osx 10.5.7上使用git

 提示> git --version 
git版本1.6.0.4
提示>






UPDATE2:JakubNarębski的答案是:

 提示> git show HEAD ^:dir1 / dir2 / dir3 / main.cpp> old_main.cpp 
提示>






UPDATE3:Karmi的回答,

 提示> git show 4c274dd91dc:higgs / Higgs.xcodeproj / project.pbxproj> old_project.pbxproj 
提示>


解决方案

您可以使用git show / p>

 提示> git show HEAD ^:main.cpp> old_main.cpp 

(请注意冒号为 HEAD ^ 和main.cpp`之间的字符。< revision> < path> 的语法在 git rev-parse 联机帮助页,位于指定修订部分的最后一点旁边:



  • < rev>:<路径>例如HEAD:README,:README,master:./ README $ b


    后缀后面跟着一个路径,指定在冒号前面部分命名的tree-ish对象中给定路径上的blob或tree。 :path (在冒号前有一个空的部分)是下面描述的语法的一个特例:记录在给定路径索引处的内容。

    ./ ../ 是相对于当前的工作目录。给定的路径将被转换为相对于工作树的根目录。这对于处理与工作树具有相同树结构的提交或树来说是最有用的。




请注意,此处的<路径> FULL 路径相对于顶部目录,即 .git / 目录的目录。 (或者更确切地说,< revision> (它通常可以是任何< tree-ish> ,即表示树的东西) p>

如果要使用相对于当前目录的路径,则需要使用./<path>语法(或../<path>从当前目录上去)。
$ b

编辑2015-01-15:

strong>添加了关于相对路径语法的信息




在大多数情况下,级别(管道) git cat-file 命令:

 提示> git cat-file blob HEAD ^:main.cpp> old_main.cpp 


I have the file "main.cpp" open in my editor.

I want to see the previous revision of "main.cpp" in the editor too.

The way I do it now is like this.

close "main.cpp" in the editor

prompt> mv main.cpp tmp
prompt> git checkout HEAD^ main.cpp
prompt> mv main.cpp old_main.cpp
prompt> mv tmp main.cpp
prompt>

open "main.cpp" and "old_main.cpp" in the editor

Can it be simplified, so I don't have to close "main.cpp" in the editor?

What I'm hoping for is a variant of git-checkout that can do this.


UPDATE: im using git on mac osx 10.5.7

prompt> git --version
git version 1.6.0.4
prompt> 


UPDATE2: Jakub Narębski answer is:

prompt> git show HEAD^:dir1/dir2/dir3/main.cpp > old_main.cpp
prompt>


UPDATE3: Karmi's answer, for a specific revision:

prompt> git show 4c274dd91dc:higgs/Higgs.xcodeproj/project.pbxproj > old_project.pbxproj
prompt> 

解决方案

You can use "git show" for that:

prompt> git show HEAD^:main.cpp > old_main.cpp

(Note that there is colon ':' character between HEAD^ and main.cpp`.) The "<revision>:<path>" syntax is described in git rev-parse manpage, next to last point in the "Specifying revisions" section:

  • <rev>:<path>, e.g. HEAD:README, :README, master:./README

    A suffix : followed by a path names the blob or tree at the given path in the tree-ish object named by the part before the colon. :path (with an empty part before the colon) is a special case of the syntax described next: content recorded in the index at the given path.

    A path starting with ./ or ../ is relative to the current working directory. The given path will be converted to be relative to the working tree’s root directory. This is most useful to address a blob or tree from a commit or tree that has the same tree structure as the working tree.

Note that "<path>" here is FULL path relative to the top directory of your project, i.e. the directory with .git/ directory. (Or to be more exact to "<revision>" (which in general can be any <tree-ish>, i.e. something that represents tree))

If you want to use path relative to the current directory, you need to use "./<path>" syntax (or "../<path>" to go up from current directory).

Edit 2015-01-15: added information about relative path syntax


You can get in most cases the same output using low-level (plumbing) git cat-file command:

prompt> git cat-file blob HEAD^:main.cpp > old_main.cpp

这篇关于git-checkout使用新名称的旧文件修订版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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