You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Mistakes happen. Git provides several tools to undo changes at every stage of the workflow — from unstaged edits all the way to committed history. Knowing the right tool for each situation prevents panic and preserves your work.
To throw away edits in your working directory that you have not yet staged:
# Discard changes to a specific file
git restore README.md
# Discard all unstaged changes in the project
git restore .
This is destructive — the edits are gone immediately with no way to recover them.
If you staged a file by mistake and want to remove it from the staging area without losing the edits:
git restore --staged README.md
The file returns to the modified but unstaged state.
To fix the most recent commit (message typo, forgotten file) before pushing:
git add missed-file.js
git commit --amend -m "Corrected commit message"
Important: Only amend commits that have not been pushed to a shared remote, because amending rewrites history.
git revert creates a new commit that undoes the changes introduced by an earlier commit. It is the safe way to undo published history because it does not rewrite anything:
git revert abc1234
Git creates a new commit with a message like Revert "Add login feature". The original commit remains in the history.
git reset moves the current branch pointer to an earlier commit. There are three modes:
# Soft: moves HEAD but keeps changes staged
git reset --soft HEAD~1
# Mixed (default): moves HEAD and unstages changes, edits remain on disk
git reset HEAD~1
# Hard: moves HEAD and discards all changes — use with caution
git reset --hard HEAD~1
HEAD~1 means "one commit before the current HEAD". Never use --hard on commits you have already pushed to a shared remote — it forces others to reconcile diverged history.
If you accidentally reset too far, the reflog can save you. It records every position HEAD has been at:
git reflog
Find the SHA of the commit you want to recover, then reset back to it:
git reset --hard abc1234
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.