Git 常用命令速查与协作工作流全指南
1. 仓库初始化(Getting a Git Repository)#
1.1 工作区 / 暂存区 / 仓库#
| 区域 | 时机 | 存储位置 |
|---|---|---|
| 工作区(Working Directory) | add 前 | 磁盘文件系统 |
| 暂存区(Staging Area / Index) | add 后、commit 前 | .git/index |
| 本地仓库(Local Repository) | commit 后 | .git/objects |
| 远程仓库(Remote Repository) | push 后 | 远端服务器 |
1.2 初始化与远端配置#
| 命令 | 说明 | 示例 |
|---|---|---|
| git init | 将当前目录初始化为 Git 仓库 | git init |
| git clone | 克隆远端仓库到本地 | git clone git@github.com:user/repo.git |
| git remote add origin | 为本地仓库绑定远端地址 | git remote add origin git@github.com:user/repo.git |
| git remote add upstream | 关联上游原始仓库 | git remote add upstream git@github.com:original/repo.git |
| git remote set-url | 修改已有远端地址 | git remote set-url origin git@github.com:user/repo-v2.git |
| git remote -v | 查看所有已配置的远端 | git remote -v |
2. 记录变更(Recording Changes)#
2.1 暂存与提交#
| 命令 | 说明 | 示例 |
|---|---|---|
| git status | 查看工作区与暂存区状态 | git status |
| git add <file> | 暂存指定文件 | git add README.md |
| git add . | 暂存全部变动 | git add . |
| git diff | 查看工作区未暂存的改动 | git diff |
| git diff –staged | 查看已暂存但未提交的改动 | git diff –staged |
| git commit -m | 提交并附说明 | git commit -m “feat: 初始化工程结构” |
| git commit –amend | 修改最近一次提交(消息或内容) | git commit –amend |
2.2 Commit 语义化规范(Conventional Commits)#
| Type | 用途 |
|---|---|
| feat | 新增功能 |
| fix | 修复 Bug |
| docs | 文档变更 |
| refactor | 代码重构(无功能增减) |
| perf | 性能优化 |
| chore | 构建/依赖/工具变更 |
格式:<type>[scope]: <description>,示例:feat(user): 新增头像上传接口
3. 查看历史(Viewing the Commit History)#
3.1 日志与差异#
| 命令 | 说明 | 示例 |
|---|---|---|
| git log | 查看完整提交历史 | git log |
| git log –oneline | 每条提交一行精简显示 | git log –oneline -10 |
| git log –graph | 图形化展示分支合并历史 | git log –oneline –graph –all |
| git show <hash> | 查看指定提交的详细改动 | git show a1b2c3d |
4. 撤销操作(Undoing Things)#
4.1 丢弃修改与取消暂存#
| 命令 | 说明 | 示例 |
|---|---|---|
| git restore <file> | 丢弃工作区未暂存的改动 | git restore style.css |
| git restore . | 丢弃工作区全部改动 | git restore . |
| git restore –staged <file> | 取消暂存(反向 add) | git restore –staged README.md |
| git rm –cached | 移除追踪,保留本地文件 | git rm -r –cached target/ |
4.2 版本回退(reset)#
| 命令 | 说明 | 示例 |
|---|---|---|
| git reset HEAD~1 | mixed(默认):撤销 commit,改动回工作区 | git reset HEAD~1 |
| git reset –soft HEAD~1 | soft:撤销 commit,改动保留在暂存区 | git reset –soft HEAD~1 |
| git reset –hard HEAD~1 | hard:彻底抹除 commit + 改动(不可恢复) | git reset –hard HEAD~1 |
4.3 安全撤销与临时搁置#
| 命令 | 说明 | 示例 |
|---|---|---|
| git revert <hash> | 生成反向提交,安全撤销不改历史 | git revert a1b2c3d |
| git stash | 临时搁置工作区改动 | git stash |
| git stash pop | 恢复最近一次搁置 | git stash pop |
| git cherry-pick <hash> | 摘取指定提交到当前分支 | git cherry-pick a1b2c3d |
5. 远程操作(Working with Remotes)#
5.1 推送与拉取#
| 命令 | 说明 | 示例 |
|---|---|---|
| git fetch | 拉取远端变更,不合并 | git fetch origin |
| git pull | 拉取并合并远端(fetch + merge) | git pull origin main |
| git push -u | 首次推送并绑定追踪分支 | git push -u origin main |
| git push | 推送已追踪的分支 | git push |
| git push –force-with-lease | 安全强推,避免覆盖他人提交 | git push –force-with-lease origin feat/x |
6. 分支(Branching)#
6.1 分支管理#
| 命令 | 说明 | 示例 |
|---|---|---|
| git branch | 列出所有本地分支 | git branch |
| git branch -vv | 查看本地分支与远端追踪状态 | git branch -vv |
| git switch <branch> | 切换到已有分支(现代语法) | git switch main |
| git switch -c <branch> | 创建并切换新分支(现代语法) | git switch -c feat/user-profile |
| git checkout -b <branch> | 创建并切换新分支(经典语法) | git checkout -b feat/user-profile |
| git branch -d <branch> | 删除已合并的本地分支 | git branch -d feat/user-profile |
| git branch -D <branch> | 强制删除未合并分支 | git branch -D feat/wip |
| git push origin –delete <branch> | 删除远端分支 | git push origin –delete feat/user-profile |
| git remote prune origin | 清理本地残留的已删除远端分支追踪 | git remote prune origin |
7. 变基(Rebasing)#
7.1 变基命令#
| 命令 | 说明 | 示例 |
|---|---|---|
| git merge –no-ff <branch> | 非快进合并,保留完整分支历史 | git merge –no-ff feat/login |
| git rebase <branch> | 将当前分支变基到目标分支 | git rebase origin/main |
| git rebase –continue | 冲突解决后继续变基 | git rebase –continue |
| git rebase –abort | 放弃变基,恢复原状 | git rebase –abort |
Important
黄金法则:严禁对公共主分支执行 rebase;仅在个人未合入的特性分支上使用。
8. GitHub 协作工作流(GitHub Workflow)#
8.1 特性分支流水线#
1# 1. 从最新主干派生分支
2git switch main && git pull origin main
3git switch -c feat/user-profile
4
5# 2. 开发 → 提交
6git add . && git commit -m "feat(user): 新增头像修改接口"
7
8# 3. 同步主干变基(有冲突则解决后 --continue)
9git fetch origin && git rebase origin/main
10
11# 4. 推送并发起 PR
12git push -u origin feat/user-profile
13
14# 5. 审查期间补充提交 → 安全强推
15git commit -m "fix(user): 修复昵称长度校验"
16git push --force-with-lease origin feat/user-profile
17
18# 6. 合入后清理
19git switch main && git pull origin main
20git branch -d feat/user-profile && git remote prune origin
8.2 GitHub CLI(gh)速查#
| 命令 | 说明 | 示例 |
|---|---|---|
| gh auth login | 账号认证 | gh auth login |
| gh pr create | 发起 PR | gh pr create –base main –head feat/x –title “feat: xxx” |
| gh pr list | 列出所有 PR | gh pr list |
| gh pr view <id> –web | 浏览器打开 PR | gh pr view 1 –web |
| gh pr checkout <id> | 检出他人 PR 到本地 | gh pr checkout 1 |
| gh pr checks | 查看 CI 状态 | gh pr checks |
| gh pr merge <id> –squash | Squash 合并 | gh pr merge 1 –squash |
8.3 PR 描述模板#
1## 概述
2- **目的**:解决了什么问题 / 新增了什么功能
3- **关联 Issue**:Closes #123
4
5## 改动内容
6- [模块名]:改动点 1
7- [模块名]:改动点 2
8
9## 验证
10- [x] 单元测试通过
11- [x] 本地联调通过
12- [ ] 边缘测试完成
13
14## 注意事项
15- **数据库**:是否需执行变更脚本
16- **Breaking Changes**:是否有不兼容调整
continue reading