Roadmap › Git & GitHub — Version Control from Basics to Advanced › Module: Undo, Inspect and Advanced Git
Lessons in this course (10)
8. Undoing things — restore, reset, revert, reflog
Intermediate★ Must knowLesson 8 of 10
| Situation | Command |
|---|---|
| Discard changes in a file | git restore Program.cs |
| Unstage a file | git restore --staged Program.cs |
| Fix the last commit message / add a forgotten file (not pushed) | git commit --amend |
| Undo last commit, keep changes staged | git reset --soft HEAD~1 |
| Undo last commit, keep changes unstaged | git reset HEAD~1 (mixed, default) |
| Throw away last commit and changes | git reset --hard HEAD~1 (dangerous) |
| Undo a commit that is already pushed/shared | git revert <sha> — new commit that reverses it |
| "I lost a commit / branch" | git reflog → find the sha → git switch -c rescue <sha> |
| Put work aside | git stash push -m "wip" → git stash list → git stash pop |
reset vs revert: reset moves the branch pointer (rewrites history — only for local commits); revert adds a new commit (safe for shared history).