Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 955
Scott Hernandez Avatar asked Feb 27 '10 17:02

Scott Hernandez


People also ask

How do I reset the last commit in Git?

Mixed reset Git commit In order to undo the last Git commit, keep changes in the working directory but NOT in the index, you have to use the “git reset” command with the “–mixed” option. Next to this command, simply append “HEAD~1” for the last commit. $ git reset --mixed HEAD~1

Did you lose something important during a Git reset?

Perhaps you did a git reset and then realized you actually lost something important? Maybe you deleted an entire branch thinking all the work was done and merged, only to discover a commit missing in master. Luckily, you can usually go back through your git reflog and scan through it until you find the commit you’re looking for.

How do I undo a commit in Git?

"Undo" the given commit or commit range. The reset command will "undo" any changes made in the given commit. A new commit with the undo patch will be committed while the original commit will remain in the history as well. # Add a new commit with the undo of the original one. # The <sha-1> can be any commit (s) or commit range git revert <sha-1>

How do I find a deleted commit in a branch?

Maybe you deleted an entire branch thinking all the work was done and merged, only to discover a commit missing in master. Luckily, you can usually go back through your git reflog and scan through it until you find the commit you’re looking for. The reflog command in git is very powerful.


2 Answers

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.

like image 108
Benjamin Manns Avatar answered Nov 22 '22 15:11

Benjamin Manns


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.

like image 32
Dustin Avatar answered Nov 22 '22 15:11

Dustin