Lessons · Git · getting a lost commit back
Getting a lost commit back
The reflog names where the branch was before the move that lost it. Point something at that, and the commit is back in the history.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Every reset, rebase and amend that goes wrong ends here, and the whole rescue is two commands.
How to think about it
Name it with a branch first and look; reset the branch only when you are sure. A name costs nothing and moves nothing.
Worked example
git log --onelineOne commit. The other is not in the history any more.
git reflogBut it is here, one move back.
git branch rescue HEAD@{1}Named. Nothing else has changed.git log --oneline rescueBoth commits, on a branch you can read before deciding.
git reset --hard rescueAnd now main is back where it was.
cat f.txttwo. The work is here.
Your turn
Put the current branch back to where the rescue branch points.
git reset rescue
Try it in a real repository
The trap
Doing the reset first, from memory. If the hash is wrong you now have two problems, and the second one is also in the reflog.
Practise getting a lost commit back on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.