Scott Hernandez
Scott Hernandez

Reputation: 7590

Lost Last Git Commit

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 :(

Upvotes: 13

Views: 4268

Answers (3)

Benjamin Manns
Benjamin Manns

Reputation: 9148

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 in Windows.

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

Upvotes: 27

Dustin
Dustin

Reputation: 91050

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.

Upvotes: 22

Tronic
Tronic

Reputation: 10430

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.

Upvotes: 7

Related Questions