shellstreak

Undo the last commit and throw its changes away

Destructive: deletes work

Short answer

Run git reset --hard HEAD~1. The commit and its changes are gone from your branch and your files (and any uncommitted edits too).

--hard also wipes any uncommitted edits in tracked files, with no undo for those. Run git status first. The commit itself can still be rescued for a while with git reflog (see I reset or deleted commits by mistake). Never use it on commits you've pushed.

The commands, one by one

  1. 1git status

    Look first: anything listed as modified would be wiped too. Commit or stash it if you want to keep it.

  2. 2git reset --hard HEAD~1

    Moves the branch back one commit and makes your files match that commit exactly. Git prints HEAD is now at… with the commit you're back on.

Try it safely

Your last commit, Try lard in scones, was a bad idea and you never pushed it.

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 61, reset --soft, --mixed, --hard, 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.