1. Undoing Changes
git restore <file>What it doesCancels unstaged modifications in the file and returns it to the last commit
When to use itto undo an unsuccessful attempt on a specific file
Example
git restore src/app.jsgit restore --staged <file>What it doesRemoves the file from the Staging Area while keeping modifications to the file locally
When to use itWhen you added a file with git add by mistake and want to unprepare it
Example
git restore --staged src/secret.envgit reset --soft HEAD~1What it doesGoes one commit back while keeping the changes stored in Staging
When to use itTo edit a message or add a file to commit that was just saved locally
Example
git reset --soft HEAD~1git reset --mixed HEAD~1What it doesOne commit goes back and returns files to the unequipped Working Tree (default)
When to use itto dismantle commit and reconfigure it another way
Example
git reset --mixed HEAD~1git reset --hard HEAD~1What it doesMoves the branch back and clears all changes from Index and Working Tree (dangerous)
When to use itto completely delete the last commit and its changes forever
Example
git reset --hard HEAD~1git revert <commit>What it doesCreates a new commit that safely reverses and undoes the effect of a previous commit
When to use itto undo an existing change on a shared public branch without rewriting history
Example
git revert a1b2c3dgit commit --amend -m "new msg"What it doesReplaces the last local commit with a new message or merges newly prepared files
When to use itTo correct a typo in the last commit message or add a file you forgot
Example
git commit --amend -m "docs: clarify recovery steps"git reflog -10What it doesDisplays the history of the HEAD cursor movement for recovering deleted commits or branches
When to use itA safety net for recovering a lost code after a wrong reset or failed rebase
Example
git reflog -10