Git 常用命令速查
常用命令
git status
git add -p
git commit -m 'feat: add search'
git diff
git diff --staged
git log --oneline --graph --decorate --all
git switch -c feature/login
git switch main
git branch -vv
git fetch --prune
git pull --ff-only
git push -u origin feature/login
git merge main
git rebase -i main
git stash push -u -m 'wip'
git stash pop
git tag -a v1.0.0 -m 'release 1.0.0'
git push origin v1.0.0
场景速查
| 目的 |
命令 |
| 撤销未暂存改动 |
git restore <file> |
| 取消暂存 |
git restore --staged <file> |
| 修改最后一次提交 |
git commit --amend |
| 撤销已推送提交 |
git revert <sha> |
| 找回误 reset |
git reflog + git reset --hard HEAD@{n} |
| 挑选提交 |
git cherry-pick <sha> |
| 查看某行历史 |
git blame <file> |
| 同步 fork |
git fetch upstream && git rebase upstream/main |
易错点
git reset --hard 会丢弃未提交改动;先确认或 stash。
- 已共享的历史不要 rebase;必须改写时用
--force-with-lease 并通知团队。
git pull 的默认合并/rebase 行为取决于配置;脚本里显式 --ff-only 或 --rebase。
.gitignore 只对未跟踪文件生效;已跟踪文件要先 git rm --cached。
- 冲突解决后要
git add,再 merge --continue 或 rebase --continue。
- 大文件不要直接提交;用 LFS 或外部存储。
- 提交前跑测试和 hooks;
--no-verify 只用于明确的紧急情况。
相关命令
git — 完整命令页
git-rebase / git-reflog / git-lfs — 进阶用法
commitlint — 提交信息规范