Git revert reset

I just made a git reset--hard command by accident. Can this git action somehow be reverted? I want to obtain back the uncommitted changes. I am using git from the command line.

Can I obtain the code from a still running tomcat/grails instance?


If the changes were not committed or staged, the answer is no.

If you committed changes, you can find back since the commit objects are there.

If you staged your changes (using git add ), you can still find back your changes, because the blob objects for your changes have been generated.

First of all, please copy your whole git repository as a backup.

Just for investigation, you can ls -l -t .git/objects to find these objects by their creating time. Each object is stored with the first 2 characters of the object id as folder name, and the left part as file name. For example, 78/981922613b2afb6025042ff6bd878ac1994e85'. And you can use 78/981922613b2afb6025042ff6bd878ac1994e85'. And you can use git cat-file` to get it's content.

git cat-file -p 78981922613b2afb6025042ff6bd878ac1994e85

Or just with the short name (first several characters of the id).

git cat-file -p 789819

To do recovery, I just found an answer for you. Please refer to Undo git reset --hard with uncommitted files in the staging area.


If the changes were never committed, the answer is no, you can't.

If you did a hard reset and lost committed changes, you can get them back by inspecting the reflog.


There is no way to get back uncommitted changes until you had not make a temporary patch before "git reset --hard" now you can "git pull" to synchronous.

But there is some tricky way you can get your uncommitted changes only if you have did some following commands before "Git reset --hard" : 1. git diff and to exit from that if you had press ctrl+z

then you can get that back by command "fg". because ctrl+z will make the git diff command running at background. if you had not done above before "Git reset --hard", sorry you have to write changes again. better luck.

链接地址: http://www.djcxy.com/p/4030.html

上一篇: 由于Adobe Brackets中的Git而丢失了所有文件

下一篇: Git恢复重置