既保留学习路线感,又能快速查命令,还加入了高频易忘命令和新版本推荐写法。
Git 速查手册
1. 初始配置
git config --global user.name "Your Name" # 设置用户名
git config --global user.email "your.email@example.com" # 设置邮箱
git config --list # 查看配置
git config --global core.editor "code --wait" # 设置默认编辑器
git config --global alias.st "status -s" # 创建别名
2. 仓库操作
git init # 初始化仓库
git clone <url> # 克隆仓库
git clone -b <branch> <url> # 克隆指定分支
3. 基本工作流
git status # 查看状态
git add <file> # 添加文件
git add . # 添加所有修改
git add -p # 按补丁交互式添加
git commit -m "提交信息" # 提交修改
git commit --amend # 修改最后一次提交
4. 查看历史
git log # 完整提交记录
git log --oneline # 简洁日志
git log --graph --decorate --oneline --all # 图形化查看所有分支
git show <commit> # 查看某次提交详情
git blame <file> # 查看文件修改人/时间
git reflog # 查看 HEAD 历史移动记录
5. 分支管理
git branch # 查看本地分支
git branch -r # 查看远程分支
git branch <name> # 创建分支
git checkout <branch> # 切换分支(旧)
git switch <branch> # 切换分支(推荐)
git checkout -b <branch> # 创建并切换(旧)
git switch -c <branch> # 创建并切换(推荐)
git merge <branch> # 合并分支
git branch -d <branch> # 删除分支
git branch -D <branch> # 强制删除
6. 远程操作
git remote -v # 查看远程仓库
git remote add origin <url> # 添加远程仓库
git fetch --prune # 同步远程分支列表
git pull # 拉取更新
git pull --rebase # 拉取并保持线性提交
git push -u origin <branch> # 首次推送分支
git push # 推送修改
git push origin --delete <branch> # 删除远程分支
git push origin --tags # 推送所有标签
7. 撤销与恢复(危险命令标注)
git restore <file> # 撤销工作区修改
git restore --staged <file> # 取消暂存
git reset HEAD <file> # 取消暂存(旧版本)
git reset --hard <commit> # 丢弃所有修改并回到指定提交
git reset --soft <commit> # 回退到指定提交,保留修改
git revert <commit> # 创建撤销提交
8. 储藏与标签
git stash # 储藏当前修改
git stash list # 查看储藏
git stash apply # 恢复储藏
git stash pop # 恢复并删除储藏
git tag # 查看标签
git tag -a v1.0 -m "版本1.0" # 创建带注释标签
9. 高级操作
git diff # 查看未暂存修改
git diff --cached # 查看已暂存修改
git diff branch1..branch2 # 比较分支差异
git cherry-pick <commit> # 应用某个提交
git merge --abort # 取消合并
git rebase -i HEAD~3 # 交互式变基最近3次提交
git rebase --abort # 取消变基
git rebase --continue # 解决冲突后继续变基
git submodule add <url> # 添加子模块
git submodule update --init --recursive # 初始化子模块
10. 最佳实践
- 提交规范:遵循 Conventional Commits(feat, fix, docs, style, refactor...)
- 分支策略:
- main / master 保持稳定可发布
- feature/* 开发新功能
- release/* 准备发布
- hotfix/* 紧急修复
- 日常习惯:
- 小步提交,清晰信息
- 推送前先 git pull --rebase
- 频繁同步远程,避免大冲突
这样整理后,既有学习路线(从配置 → 基础 → 分支 → 高级),又保留了速查手册的快速定位功能,而且全部是最新版 Git 推荐用法,危险命令也标了。