World map

 

BM: Baca dalam Bahasa Melayu

World 11: Rescue Center

Undo my last commit

You will learn: the safe undo for your last commit, pushed or not.

The problem

git log --oneline: Add day 2 entry sits on top, above Add style, Add day 1 page and Add README. Terminal output: $ git log --oneline bcf4670 (HEAD -> main) Add day 2 entry 270a76e Add style a8feb63 Add day 1 page 6d3a9b1 Add README

You committed day 2 too soon. It isn’t ready yet. Can you take the commit back? Yes! This happens to everyone.

Try it

You haven’t pushed. You want to keep the changes. Which undo?

Pick one answer.

Watch

main and HEAD move back one commit, to Add style. Add day 2 entry leaves the line.
6d3a9b1a8feb63270a76ebcf4670

git reset --soft HEAD~1 takes the last commit back. Nothing is lost: the changes sit in the box, ready for later.

Try it

Take back the day 2 commit. Keep the changes in the box.

Working folder
  • Empty
Staging area
  • Empty
Repository
  • 4 save points
  • Add day 2 entry
  • Add style
  • Add day 1 page

Predict

New story: the commit is pushed. Teammates have it. Which undo is safe?

Pick one answer.

Try it

Undo the pushed commit the safe way. Then push the undo.

6d3a9b1a8feb63270a76ebcf4670

You did it!

You can now undo your last commit, pushed or not.

Go deeper (optional)

--soft keeps changes in the box. Plain git reset HEAD~1 keeps them on your desk, out of the box. --hard wipes them, so use it only for changes you truly don’t want. The commit itself stays findable. See “Reflog, your safety net” in World 2.