views:

178

answers:

3

I lost my last commit because I accidentally ran "git reset --hard HEAD^". Note: I didn't want to put the "^" at the end.

Is there any way to get it back? It was 2 days of work :(

+4  A: 

If you know the commit ID (e.g. scroll back on your terminal or use git reflog),

git reset --hard 61567de5d9

Where 61567de5d9 are the first digits of the latest (lost) commit.

Tronic
+8  A: 

I think that this article is what you are looking for. According to the article, your commit is "gone," but not garbage collected - sort of like the recycle bin.

You run git fsck --lost-found to find the 'dangling commit', and looking at it with git reflog, then merging the dangling commit with your current branch, git merge 7c61179.

Benjamin Manns
Thanks. That did it. I knew it was just floating around in there someplace, unlinked from the branch.
Scott Hernandez
+8  A: 

git makes it really easy to go back to a prior state and works very hard to prevent you from losing any data you've committed. It's this reason you should commit often. I've got a command git trash that does that git reset --hard state, but after writing a commit so that I can undo the hard reset if I need.

For the most recent state (i.e. your exact case), just do git reset --hard ORIG_HEAD to undo what you just did.

You can do a time-based reset: git reset --hard '@{5 minutes ago}' to put yourself in a prior state based on time (there are lots of options you can use, for example, git reset --hard '@{yesterday}' to pretend today never happened).

Otherwise, browse the git reflog output to find the thing before the action you feel put you in a bad state and reset to that.

Dustin
thanks for your comments. it is useful to me.
Nadal
More general you can use `@{1}`, or `HEAD@{1}` instead of `ORIG_HEAD`.
Jakub Narębski