git-checkout 以新名称对文件的旧版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/888414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:29:01  来源:igfitidea点击:

git-checkout older revision of a file under a new name

git

提问by neoneye

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

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

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

我也想main.cpp在编辑器中看到“ ”的先前修订版。

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?

是否可以简化,这样我就不必在编辑器中关闭“main.cpp”?

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

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



UPDATE: im using git on mac osx 10.5.7

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

prompt> git --version
git version 1.6.0.4
prompt> 


UPDATE2: Jakub Nar?bski answer is:

更新 2:Jakub Nar?bski 的回答是:

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


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

UPDATE3:Karmi 的回答,针对特定修订:

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

回答by Jakub Nar?bski

You can use "git show" for that:

您可以使用“git show”:

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-parsemanpage, next to last point in the "Specifying revisions" section:

(请注意,和:之间有冒号 [ ] 字符。)git rev-parse联机帮助页中描述了语法,在“指定修订版”部分的最后一点旁边:HEAD^main.cpp<revision>:<path>

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

  • <rev>:<path>,例如 HEAD:README, :README, master:./README

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

    ./../相对于当前工作目录开头的路径。给定的路径将被转换为相对于工作树的根目录。这对于从与工作树具有相同树结构的提交或树中寻址 blob 或树最有用。

Note that <path>here is FULLpath relative to the top directoryof 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))

请注意,<path>这里是相对于项目顶级目录完整路径,即带有目录的目录。(或者更准确地说是“ <revision>”(通常可以是任何<tree-ish>,即代表树的东西)).git/

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

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

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

编辑 2015-01-15:添加了有关相对路径语法的信息



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

在大多数情况下,您可以使用低级(管道)git cat-file命令获得相同的输出:

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

回答by karmi

Just to add to Jakub's answer: you don't even have to redirect the output to a file with >, if you are only interested in skimming the file contents in the terminal. You can just run $?git show 58a3db6:path/to/your/file.txt.

只是添加到 Jakub 的答案中:>如果您只对浏览终端中的文件内容感兴趣,您甚至不必将输出重定向到带有 的文件。你可以运行$?git show 58a3db6:path/to/your/file.txt