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
SituationCommand
Discard changes in a filegit restore Program.cs
Unstage a filegit restore --staged Program.cs
Fix the last commit message / add a forgotten file (not pushed)git commit --amend
Undo last commit, keep changes stagedgit reset --soft HEAD~1
Undo last commit, keep changes unstagedgit reset HEAD~1 (mixed, default)
Throw away last commit and changesgit reset --hard HEAD~1 (dangerous)
Undo a commit that is already pushed/sharedgit revert <sha> — new commit that reverses it
"I lost a commit / branch"git reflog → find the sha → git switch -c rescue <sha>
Put work asidegit 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).