Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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>  
like image 599
neoneye Avatar asked May 20 '09 14:05

neoneye


People also ask

Which command is used to explore the previous versions of a project?

git checkout recovers old versions of files.


2 Answers

You can use git show for that:

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:

git cat-file blob HEAD^:main.cpp > old_main.cpp 
like image 111
Jakub Narębski Avatar answered Oct 11 '22 22:10

Jakub Narębski


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.

like image 21
karmi Avatar answered Oct 11 '22 23:10

karmi