deb0rian
deb0rian

Reputation: 976

How to revert "git reset" changes?

Soooo.... I did "git reset HEAD myfilename" and it deletes all my local changes. I don't think I did any commits of it to a local repo.

Is there a way to restore deleted code? :L

Upvotes: 0

Views: 138

Answers (2)

torek
torek

Reputation: 489718

git reset HEAD filename just clears out cached changes in the index (basically, undoes git add). If you had changes in the working copy, and you no longer have them, something else did them in.

When you did:

git checkout -- js/ModuleVk.js

this grabbed the HEAD version of js/ModuleVk.js, wrote that into the index, and then wrote that into the work tree. So that could have clobbered changes.

If you did do a git add before this, though, the repo contains a copy of the working file at the time the add occurred. You can find this with git fsck --lost-found, which will print some number of messages like this:

dangling commit 7a07e53b3c753c93303c881605c81cc476c6a1de
dangling blob 8177212592bb88a3c4614ee288d6a8a01dee0b42
dangling commit 83630458b4317f5db42c3a306e299ca8c26a7ed9
dangling blob b50f361891f38b85a5733b3124e261654ed457ad
dangling blob b67bea2cf13d558e4092e9c1939b2273ac904f83

When it's all done your .git directory will have a sub-directory named lost-found, which in turn will have two sub-directories named commit and other.

The files in other all have SHA-1 names, so you have to just look at them manually. It should be obvious which ones are .js code, but figuring out which one is which git add-ed version can be tricky, depending on how much stuff you've git added but not actually committed, over time. (I habitually git add stuff, git diff --cached, decide to change it some, git add again, and so on. Each of those puts another "dangling blob" into the repository. They're eventually gc'ed, and disk space is cheap....)

Once you're done with the files in there, you can manually remove everything from .git/lost-found:

rm -r .git/lost-found

(you can regenerate them later with another git fsck --lost-found, although once the garbage collector reaps dangling objects they're really gone for good—but by default that takes at least two weeks for "fresh" objects).

Upvotes: 3

Aaron Digulla
Aaron Digulla

Reputation: 328770

No, if the files are gone, then they're gone.

If you use Eclipse, you can still find them in the local history (try Compare ... / Local History ...)

Upvotes: 1

Related Questions