Horatio Mnml
Horatio Mnml

Reputation: 19

Restore files from git

I worked on a new Projekt. I added all the Files to my local git-repository

git add .

but i didn't commit it. Then i accidently resetet the git

git reset --hard

How is it possible to restore my Files? I hoped, that atom kept the files, but also there, the files where not open anymore. Nevermind, that atom would close all my files.

Upvotes: 1

Views: 471

Answers (1)

larsks
larsks

Reputation: 311586

When you git add a file, git stores it as an object in your repository (in the .git directory). Your files are probably still hanging around. On the other hand, by running the git reset --hard command you have lost any information about the file names.

You can see the objects themselves by running:

find .git/objects -type f

Which will produce something like:

.git/objects/67/6e8ff99e42b61f96bb0107fa1dff59594eba07
.git/objects/7c/45d8f7d987c584a8d61a8d75431ddd4f1ba52f

You can see the contents of those objects with the git cat-file command, as in:

git cat-file -p 7c45d8f7d987c584a8d61a8d75431ddd4f1ba52f

Note that the object id is the concatenation of the two characters in the first level directory followed by all the characters of the filename.

So it would be fairly trivial to write a script that would restore the contents of all your files, but you would have to manually rename them after the fact.

Upvotes: 4

Related Questions