Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check out a particular version of one file in Git?

Tags:

git

How can I check out a particular version of one file in git?

I found this mail on the mailing list, which said:

$ git checkout HEAD~43 Makefile $ git reset Makefile 

But I don't understand how to find out 'HEAD~43', if I do a git log aFile, how can I find out which 'HEAD~43' I should use?

And why do I need to run git reset for that file? What does it do?

like image 498
n179911 Avatar asked Jul 23 '09 18:07

n179911


People also ask

How do I revert to a previous version of a file in git?

To move HEAD around in your own Git timeline, use the git checkout command. There are two ways to use the git checkout command. A common use is to restore a file from a previous commit, and you can also rewind your entire tape reel and go in an entirely different direction.


2 Answers

You know what commit (ie: the specific revision) the file belongs to? Then do:

git checkout <commit> <file> 

The other command:

git checkout HEAD~N <file> 

Is for when you want to get a version of the file from a range back (which I do for nostalgia).

like image 71
Fake Code Monkey Rashid Avatar answered Sep 25 '22 11:09

Fake Code Monkey Rashid


HEAD~43 is just treeish, so you can use a hash or a tag. You have to separate treeish from the filename with --, otherwise it is treated as filename. For example.

git checkout v0.45 -- filename git checkout HEAD^ -- filename git checkout 16bb1a4eeaa9 -- filename 
like image 36
dhill Avatar answered Sep 25 '22 11:09

dhill