How do you undo changes in Git, and how do you choose the right command?
Show answer — try answering out loud first
It depends on where the change is. To discard uncommitted edits you use git restore. To unstage something, git restore --staged. To move HEAD and undo local commits, git reset (--soft, --mixed, --hard). To undo a commit that's already been published without deleting history, git revert, which creates an inverse commit.
The key is to ask yourself: what do I want to undo, and has that change already been published?
git restore archivo: discards a file's uncommitted changes, returning it to the last commit. (Previously you usedgit checkout -- archivo.)git restore --staged archivo: unstages the file, but keeps your changes in the working directory.git reset: moves the HEAD pointer to an earlier commit. It has three modes:--soft: undoes the commit, but leaves the changes in staging.--mixed(the default): undoes the commit and unstages the changes, but keeps them in the working directory.--hard: undoes the commit and deletes the changes. Dangerous: you lose work.
git revert <commit>: creates a new commit that undoes another commit's changes. It doesn't delete history, which is why it's safe for commits that have already been shared.
Golden rule: on history that's already been published (that others have), use revert, never reset --hard.
# Discard a file's uncommitted changes (goes back to the last commit)
git restore index.html
# Unstage a file, keeping its changes
git restore --staged index.html
# Undo the LAST commit, keeping the changes in staging
git reset --soft HEAD~1
# Undo the last commit, keeping the changes unstaged (the default)
git reset HEAD~1
# equivalent to: git reset --mixed HEAD~1
# Undo the last commit AND DELETE the changes (be careful)
git reset --hard HEAD~1
# Undo an already-published commit by creating an inverse one (safe)
git revert abc1234
Using git reset --hard on a shared branch and rewriting history that others already have, causing them serious conflicts when they pull. It's also very common to confuse reset --soft, --mixed, and --hard: remember that --soft keeps changes staged, --mixed keeps them unstaged, and --hard deletes everything.
"It depends on where the change is. If I only edited a file and didn't commit it, I use
git restore. If I already staged it and want to unstage it,git restore --staged. If I already made a local commit that I want to undo, I usegit reset:--softleaves the changes staged,--mixedleaves them unstaged, and--harddeletes them. And if the commit is already published, I usegit revert, which creates an inverse commit without rewriting history. The golden rule is to never usereset --hardon shared history."
You made a local commit that you haven't pushed yet, and you want to undo it but keep the changes ready to commit again (staged). Which command do you use?
See answer
git reset --soft HEAD~1. The --soft mode undoes the commit but leaves all of its changes in the staging area, so you can edit them, re-stage whatever you want, and make a new commit. Since the commit was local and hadn't been published, rewriting that history is safe.