简体   繁体   中英

Accidentally hard reset branch on git

I accidentally used this command on my main branch

git reset --hard origin/main

and now all my work of previous 3 days gone. Unfortunately,I couldn't commit in last few days and now all the work of previous days is gone and I dont have another backup for that. Is there any way I can get it back. Yesterday I committed but could'nt push due to some error now When I use

git reflog

it is showing

b1a3b94 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: commit: IgnoreUpdated
9d9ad52 HEAD@{1}: reset: moving to origin/main
9397cfe HEAD@{2}: commit: 28122020
08e9bc4 HEAD@{3}: commit: 27122020
d7436a7 HEAD@{4}: commit: 27122020
0427a52 HEAD@{5}: commit: 27122020
e98e686 HEAD@{6}: commit: 23122020

now can i get any files back from HEAD@{3}

Unsurprisingly, you simply git reset --hard back to the commit you want. Before doing so I recommend creating a new local branch in case you'll pick the wrong commit or will change your mind.

It would be prudent to store your current state of git reflog - your next actions will add more lines in there and if you really use commit messages like "27122020", then you will have have trouble finding your way around.

git reflog > ~/my-emergency-reflog-backup.txt
git checkout -b recover-work
git reset --hard 08e9bc4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM