Restoring a file from Git?

The question is related to the question "How to restore Git after fatal file system error?", but for a single file.

I need to get to last state 2009-07-27 23:58, where I need the file:

/Users/henri/BAckup/6-relationdiagram/Normalized_perhaps_DB/simple_schema0.tcuml

How can I restore the file after a crash?

ADDED: Where will I find the file? What are they supposed to do?

Graham's tip

$git checkout 63c6844fded9cfcdee14c9330be82557046b3e56 HENRI_suunnittelu_doc/6-relaatiotietokantakaavio/Normalized_perhaps_DB/simple_schema0.tcuml

William's tip

git checkout bee6763b55cf8259438aa575cedbb09d1d02b96a  HENRI_suunnittelu_doc/6-relaatiotietokantakaavio/Normalized_perhaps_DB/simple_schema0.tcuml

Upvotes: 3

Views: 1467

Answers (3)

j2emanue
j2emanue

Reputation: 62519

sometimes i delete a file or directory in my git local and i want to restore just that file. i run the following command:

 git checkout -- filenameOrDirectory

note the spaces in between.

Upvotes: 2

William Pursell
William Pursell

Reputation: 212248

Do you have an uncorrupted copy of the repository anywhere? If so, pull from there to your working directory and then checkout the file. You can do:

git log --since='2009-07-27 23:58' --pretty=oneline -n 1

to get the hash you want and then get the file via:

git checkout <file> <hash>

If you have no uncorrupted working copies of the repository, you might try 'git fsck', but your chance of success is small or zero if files have been lost.

Upvotes: 1

Graham
Graham

Reputation: 4295

If you need to get a single file out of a commit that is in your repository then git checkout will do it for you. Specifically:

git checkout <sha> <filename>

will retrieve the file <filename> from the commit <sha> into your current working copy. can be any reference to any commit, so it could be a branch name, a tag name, HEAD^^^^, or anything at all like that that you want...

Upvotes: 4

Related Questions