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
| Scenario | Command |
|---|
| Discard working-tree changes | git restore file |
| Unstage | git restore --staged file |
| Amend last commit | git 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
| Action | Command |
|---|
| Create and switch | git switch -c name |
| List branches | git branch -a |
| Merge | git merge name |
| Rebase | git rebase name |
| Stash / pop | git stash / git stash pop |
Commits & Logs
| Action | Command |
|---|
| Stage all | git add -A |
| Commit | git commit -m "msg" |
| Compact log | git log --oneline --graph -10 |
| Show changes | git diff / git diff --staged |
Remotes
| Action | Command |
|---|
| Pull | git pull --rebase |
| Push | git push |
| Force push (careful) | git push --force-with-lease |
Sources
- Git docs (git-scm.com/doc)
最后更新:2026-08-02