Git 的命令很多,但日常真正反复用到的其实就那么几十条。这份 cheatsheet 按场景整理,方便自己随时查阅。
分支
每个功能一个分支,是保持 main 干净的前提。
# 新建并切换到分支git switch -c post/softmax-temperature
# 查看所有分支(含远程)git branch -a
# 删除已合并的本地分支git branch -d post/softmax-temperature暂存与提交
# 交互式挑选要提交的改动git add -p
# 修改上一次提交(还没 push 时)git commit --amend --no-edit
# 临时收起手头的改动git stash push -m "wip: 导航栏"git stash pop改写历史
# 把最近 3 个提交整理一下git rebase -i HEAD~3
# 把 feature 分支变基到最新的 maingit fetch origingit rebase origin/main救火
找回「丢失」的提交
# reflog 记录了 HEAD 的每一次移动git refloggit switch -c rescue <commit-hash>撤销一次已推送的提交
git revert <commit-hash>git push小结
记不住没关系,知道「有这么一条命令」并且知道去哪里查,就已经够用了。