Appearance
提交
提交(Commit)是 Git 工作流的核心操作,它将暂存区的内容永久保存到仓库历史中。
git commit 创建提交
bash
# 基本提交(会打开编辑器填写提交信息)
git commit
# 行内提交信息
git commit -m "feat: 添加用户登录功能"
# 多行提交信息
git commit -m "feat: 添加用户登录功能
实现了基于 JWT 的用户认证
支持 Google OAuth 第三方登录
添加了登录频率限制"git commit -m 行内消息
-m 参数直接在命令行指定提交信息,无需打开编辑器:
bash
git commit -m "fix: 修复登录页面的样式问题"对于多行消息,使用多个 -m:
bash
git commit -m "feat: 添加搜索功能" -m "支持全文搜索和标签过滤"git commit -a 跳过暂存
-a 参数自动暂存所有已跟踪文件的修改,跳过 git add 步骤:
bash
git commit -a -m "fix: 修复数据库连接问题"
# 等价于:
# git add -u
# git commit -m "fix: 修复数据库连接问题"注意:
-a只处理已跟踪的文件。新文件(Untracked)仍然需要先git add。
git commit --amend 修改最近提交
--amend 可以修改最近一次提交(修改提交信息或追加文件):
bash
# 只修改提交信息
git commit --amend -m "fix: 修正提交信息拼写错误"
# 追加忘记提交的文件
git add forgotten-file.js
git commit --amend --no-edit # --no-edit 保持原有提交信息不变
# 打开编辑器修改信息并包含新的暂存内容
git add forgotten-file.js
git commit --amend警告:
--amend会创建一个新的提交对象(SHA-1 改变)。如果该提交已经推送到远程,不要使用--amend,否则需要强制推送,会影响其他协作者。
提交信息规范(Conventional Commits)
Conventional Commits 是一种提交信息规范,使提交历史更清晰,并能自动生成 CHANGELOG。
格式
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]type 类型
| type | 说明 |
|---|---|
feat | 新功能 |
fix | Bug 修复 |
docs | 文档更新 |
style | 代码格式(不影响功能,如空格、分号) |
refactor | 重构(不是新功能也不是 bug 修复) |
perf | 性能优化 |
test | 添加/修改测试 |
chore | 构建过程或辅助工具的变动 |
ci | CI/CD 配置修改 |
revert | 回退某次提交 |
build | 构建系统或外部依赖变动 |
示例
bash
# 新功能
git commit -m "feat(auth): 添加 GitHub OAuth 登录"
# Bug 修复
git commit -m "fix(api): 修复分页查询时的空指针异常"
# 破坏性变更(BREAKING CHANGE)
git commit -m "feat!: 重构用户认证 API
BREAKING CHANGE: /api/login 接口参数格式已更改
旧格式: { username, password }
新格式: { email, password }"
# 文档更新
git commit -m "docs: 更新 README 安装说明"
# 重构
git commit -m "refactor(order): 提取订单状态机逻辑"
# 依赖更新
git commit -m "chore(deps): 升级 React 到 18.3.0"什么是好的提交信息
好的提交信息的特征
1. 用祈使句描述做了什么,而非描述"我做了什么"
bash
# 好
git commit -m "feat: 添加用户头像上传功能"
# 不好
git commit -m "added avatar upload"
git commit -m "我添加了头像上传功能"2. 标题行不超过 72 个字符
大多数 Git 工具(如 GitHub)会截断过长的标题行。
3. 标题和正文之间空一行
feat: 添加邮件通知功能
当用户订单状态发生变化时,自动发送邮件通知。
支持以下场景:
- 订单创建成功
- 订单发货
- 订单完成4. 正文说明"为什么",而不只是"做了什么"
fix: 修复会话超时后无法刷新 Token 的问题
之前的实现在 Token 过期时直接返回 401 错误,
导致用户需要重新登录。
改为检测到 Token 即将过期时(剩余 5 分钟内),
自动调用 refresh_token 接口续期,提升用户体验。
Closes #1235. 关联 Issue 和 PR
bash
git commit -m "fix: 修复首页加载缓慢的问题
减少了不必要的 API 调用,将首屏加载时间从 3s 降低到 0.8s。
Closes #456
See: https://github.com/org/repo/pull/789"反例对比
bash
# 糟糕的提交信息
git commit -m "fix"
git commit -m "wip"
git commit -m "update"
git commit -m "修改了一些东西"
git commit -m "asdfasdf"
# 好的提交信息
git commit -m "fix(cart): 修复购物车商品数量不同步的问题"
git commit -m "feat(user): 添加用户偏好设置功能"
git commit -m "perf(image): 使用 WebP 格式减少图片体积 40%"提交前检查
bash
# 提交前先检查将要提交的内容
git diff --cached # 查看暂存区与上次提交的差异
git status # 确认暂存区内容
# 确认无误后再提交
git commit -m "feat: ..."提交模板
可以设置提交信息模板,提醒自己写规范的提交信息:
bash
# 创建模板文件
cat > ~/.gitmessage << 'EOF'
# <type>(<scope>): <subject>
# 例如: feat(auth): 添加用户登录功能
#
# [可选] 正文:说明为什么做这个改动
#
# [可选] 尾注:关联 Issue、Breaking Change 等
# Closes #
# BREAKING CHANGE:
EOF
# 配置 Git 使用此模板
git config --global commit.template ~/.gitmessage空提交
有时需要创建一个不包含任何文件改动的提交(如触发 CI/CD):
bash
git commit --allow-empty -m "ci: 触发重新部署"总结
| 操作 | 命令 |
|---|---|
| 基本提交 | git commit -m "消息" |
| 跳过暂存 | git commit -am "消息" |
| 修改最近提交 | git commit --amend |
| 追加到上次提交 | git add file && git commit --amend --no-edit |
| 空提交 | git commit --allow-empty -m "消息" |
写好提交信息是专业开发者的基本素养。规范的提交历史是团队协作、问题追踪和自动化流程的基础。