Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: How to checkout a file from a previous commit without overwriting the current one?

Several commits previously I had some javascript working one way. Over the course of several commits I now have it working another way. I would like to look at the file as it was several commits ago because I might want to use aspects of it in the new javascript. However I don't want to revert my current file to exactly what it was. I just want to literally open the old file, have a look around it and maybe copy and paste some code from it into the new file.

How do I retrieve a copy of the file from a previous commit without overwriting the most recent version?

like image 815
punkrockbuddyholly Avatar asked Sep 21 '12 09:09

punkrockbuddyholly


1 Answers

Simply redirect the output of git show to a file.

git show *SHA-of-old-commit*:*filename* > newfilename

eg:

git show 7c4eacc:main.cpp > main.cpp.old
like image 75
thameera Avatar answered Oct 11 '22 06:10

thameera