高频 Git 指令速查。
基础提交
git status # 查看状态
git add . # 暂存
git commit -m "feat: ..." # 提交
git log --oneline -10 # 最近提交
git diff # 未暂存改动
git diff --staged # 已暂存改动
分支
git branch # 本地分支
git branch -a # 所有分支
git checkout -b feature # 新建并切换
git switch feature # 切换
git branch -d feature # 删除已合并分支
git merge feature # 合并
撤销
git restore file.txt # 丢弃工作区改动
git restore --staged file # 取消暂存
git reset --soft HEAD~1 # 撤销提交但保留改动
git reset --hard HEAD~1 # 撤销提交并丢弃改动(慎用)
git commit --amend # 修改最近提交信息
远程
git remote -v # 查看远程
git remote add origin <url> # 添加远程
git push origin main # 推送
git pull # 拉取
git fetch # 只获取不合并
git push --force-with-lease # 安全强推
变基(rebase)
git rebase main # 当前分支变基到 main
git rebase -i HEAD~3 # 交互式整理最近 3 条提交
git pull --rebase # 变基式拉取(保持线性历史)
标签
git tag v1.0.0 # 打标签
git tag -a v1.0.0 -m "..." # 带注释标签
git push origin v1.0.0 # 推送标签
git tag -d v1.0.0 # 删除本地标签
查找
git log -S "关键字" # 查找新增/删除某字符串的提交
git blame file.txt # 逐行归属
git show <commit> # 查看某次提交内容
暂存 stash
git stash # 暂存改动
git stash pop # 恢复
git stash list