Video in progress
You ran `git reset --hard` and "lost" commits you needed. How do you get them back?
git reflog keeps a local record of where HEAD and the branches have been. You find the commit hash from before the reset in it, then get it back with git reset --hard <hash> or with a new branch.
📖 Answer
Something that was committed almost never disappears in Git. It just ends up with no branch pointing at it.
git reflog
# 3c2b1a0 HEAD@{0}: reset: moving to HEAD~4
# 9f8e7d6 HEAD@{1}: commit: To'lov webhook'i uchun idempotency
# 5a4b3c2 HEAD@{2}: commit: Webhook imzosini tekshirish
# ...
git branch recovered 9f8e7d6 # the safe way: save it to a branch first
git reset --hard 9f8e7d6 # or go back directly
Another handy syntax: git reset --hard HEAD@{1} means "go back to the state one step ago". The same works after a failed rebase: ORIG_HEAD, or the pre-rebase entry in the reflog.
The limits:
- the reflog exists only in your local repository, not on the server;
- entries are kept for 90 days by default (30 days for unreachable commits);
- uncommitted changes do not make it into the reflog, so with
--hardthey really are gone.
Sign up to mark studied questions and save your progress.