Lost Last Git Commit

2020-02-06 05:52发布

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

3条回答
Luminary・发光体
2楼-- · 2020-02-06 06:02

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.

查看更多
地球回转人心会变
3楼-- · 2020-02-06 06:19

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.

查看更多
Evening l夕情丶
4楼-- · 2020-02-06 06:28

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.

查看更多
登录 后发表回答