shellstreak

Undo the last commit but keep your changes

Careful: rewrites history

Short answer

Run git reset --soft HEAD~1. The commit disappears, and everything it changed stays staged, so you can fix it and commit again.

Only for commits you haven't pushed. If others may already have the commit, use Undo a pushed commit instead.

The commands, one by one

  1. 1git reset --soft HEAD~1

    Moves your branch back one commit (HEAD~1 means one before the latest). --soft touches nothing else: the files keep your changes and they stay staged.

  2. 2git status -s

    Checks the result: the changes from the undone commit are listed as staged (A or M in the first column).

For your own repository: Undo more than one commit with HEAD~2, HEAD~3 and so on.

Try it safely

You just committed Add muffin recipe and want to take it back.

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 32, Undo the agent's mistakes, 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.