shellstreak

Recover commits after git reset --hard

Safe: nothing is lost

Short answer

Run git reflog to find where your branch was, then git reset --keep HEAD@{1} to go back there.

The commands, one by one

  1. 1git reflog

    HEAD@{0} is the reset you just did; HEAD@{1} is where you were right before it, with your commits.

  2. 2git reset --keep HEAD@{1}

    Moves the branch back to that point. --keep refuses if it would overwrite uncommitted changes.

For your own repository: If you did other things since, pick the right HEAD@{n} from the reflog list (the line before the reset).

Try it safely

Someone ran git reset --hard HEAD~2 and the muffin and cookie commits vanished.

learner@shellstreak: ~
Starting your practice shell…

Type your own commands too. Reset puts the repository back to just after the mistake.

Practise this in 5 minutes

Lesson 65, The reflog remembers, walks you through this undo and the ones around it in a safe terminal, step by step, and checks your answers.

Keep the terminal cheat sheet handy: free to print

Get the shellstreak newsletter

Short terminal and Git tips you can try in a minute, plus a heads-up when new free tools and lessons ship. It lands now and then, never a flood.

No spam, unsubscribe in one click.