Undoing changes
Discard, unstage, reset or revert — and know which one is safe to share.
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.
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| Command | What it changes | Safe on shared history |
|---|---|---|
git restore <file> | Working tree file | Yes, local only |
git restore --staged <file> | Staging area | Yes, local only |
| git reset --soft HEAD~1 | Local commit pointer | No, rewrite only locally |
git revert <sha> | Adds an inverse commit | Yes |
Your turn
EasyUndo the last commit, keep the work
Remove the most recent commit but keep its changes staged.
BASH · starter
# your command hereCheck 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 inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.