Sikau

Git & GitHub · Day 4 · about 25 min

Undoing changes

Discard, unstage and fix your last commit.

Oops, undo!

You broke a file and want yesterday's version back. Or you staged the wrong thing. Or your commit message has a typo. Each has a precise, safe fix.

The undo toolkit

git init
echo "Welcome!" > index.html
git add .
git commit -m "Add page"
echo "oops" > index.html
git restore index.html
cat index.html
echo "secret" > notes.txt
git add notes.txt
git restore --staged notes.txt
git status
git add notes.txt
git commit -m "Add nots"
git commit --amend -m "Add notes"
git log --oneline
  • Discard edits to a file → git restore file
  • Unstage a file → git restore --staged file
  • Fix the last commit's message or add a forgotten file → git add … then git commit --amend

The rest of this lesson

  • Choosing the right undoexplain
  • Amend with careexplain
  • Which one?predict
  • Which undo?match
  • 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