Git & GitHub·Fix and recover

Undoing changes

Discard, unstage, reset or revert — and know which one is safe to share.

Beginner 13 min read Lesson 8 of 480 Challenge included
What you will be able to do
  • Discard working and staged changes
  • Undo a commit locally
  • Revert a pushed commit safely

Git has several undo tools because they act on different layers: the working tree, the staging area or history. Pick the one that matches what you want to keep.

undo.shTerminal
git status
git restore notes.md            # discard working changes
git restore --staged notes.md   # unstage, keep the edit
git reset --soft HEAD~1         # undo the commit, keep it staged
git revert a1b2c3d              # new commit that undoes a pushed one
git reflog                      # find commits after a reset
CommandWhat it changesSafe on shared history
git restore <file>Working tree fileYes, local only
git restore --staged <file>Staging areaYes, local only
git reset --soft HEAD~1Local commit pointerNo, rewrite only locally
git revert <sha>Adds an inverse commitYes
Your turn

Undo the last commit, keep the work

Easy

Remove the most recent commit but keep its changes staged.

BASH · starter
# your command here
Check your understanding before moving on.
Knowledge checkQuestion 1

Which command safely undoes a commit that is already pushed?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Stashing work
Lesson 8 of 480 · 0% of course