Sikau

Git & GitHub · Day 11 · about 25 min

Undoing commits

revert is safe; reset rewrites history.

A bad commit

A commit broke the site. There are two ways back: add a new commit that undoes it (revert), or move the branch back as if it never happened (reset). Choosing correctly matters when others share your history.

revert vs reset

echo "<h1>BROKEN</h1>" > index.html
git commit -am "Experimental header"
git revert HEAD
git log --oneline
cat index.html
echo "draft" > notes.txt
git add .
git commit -m "WIP notes"
git reset HEAD~1
git status
git reset --hard HEAD
git log --oneline
  • Already pushed or shared? Use revert.
  • Only on your machine? reset is fine to tidy up.
  • git reset --hard deletes uncommitted work for good — check git status first.

The rest of this lesson

  • Choosing safelyexplain
  • Three kinds of resetexplain
  • Count the commitspredict
  • Pick the commandmatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 18 lessons of Git & GitHub