TL;DR

Wrong file? git restore. Wrong unpushed commit? git reset --soft HEAD~1. Wrong pushed commit? git revert. Switch branches with git switch.

Undo

ScenarioCommand
Discard working-tree changesgit restore file
Unstagegit restore --staged file
Amend last commitgit commit --amend
Uncommit, keep changes (unpushed)git reset --soft HEAD~1
Hard reset (unpushed, dangerous)git reset --hard HEAD~1
Revert pushed commit (safe)git revert <sha>

Branches

ActionCommand
Create and switchgit switch -c name
List branchesgit branch -a
Mergegit merge name
Rebasegit rebase name
Stash / popgit stash / git stash pop

Commits & Logs

ActionCommand
Stage allgit add -A
Commitgit commit -m "msg"
Compact loggit log --oneline --graph -10
Show changesgit diff / git diff --staged

Remotes

ActionCommand
Pullgit pull --rebase
Pushgit push
Force push (careful)git push --force-with-lease

Sources

最后更新:2026-08-02