git commit 命令
简介
git commit 是 Git 中用于将暂存区的更改提交到仓库的命令。它是 Git 工作流中的关键步骤,用于保存项目的快照。
基本概念
什么是提交
提交(commit)是 Git 仓库中的一个快照,记录了某个时间点工作目录的状态。每个提交包含:
- 提交信息(commit message)
- 作者信息
- 时间戳
- 指向父提交的指针
- 文件的完整快照
提交的作用
- 保存更改:将暂存区的更改永久保存到仓库
- 创建历史:形成项目的开发历史
- 版本控制:可以回退到任何提交
- 协作基础:团队成员可以共享提交
命令语法
1 2 3 4 5 6 7 8
| git commit [选项]
git commit -m "提交信息"
git commit -a -m "提交信息"
|
查看帮助文档
常用选项
| 选项 |
说明 |
-m, --message=<msg> |
直接指定提交信息 |
-a, --all |
自动暂存所有已跟踪文件的更改 |
--amend |
修改最后一次提交 |
-v, --verbose |
在编辑器中显示差异 |
-q, --quiet |
静默模式 |
-s, --signoff |
添加 Signed-off-by 行 |
-S, --gpg-sign |
使用 GPG 签名提交 |
--no-verify |
跳过 pre-commit 和 commit-msg 钩子 |
--allow-empty |
允许空提交 |
--no-edit |
使用上次的提交信息(amend 时) |
-e, --edit |
编辑提交信息(默认行为) |
--fixup=<commit> |
创建 fixup 提交 |
--squash=<commit> |
创建 squash 提交 |
-C, --reuse-message=<commit> |
重用指定提交的信息 |
-c, --reedit-message=<commit> |
重用并编辑指定提交的信息 |
-F, --file=<file> |
从文件读取提交信息 |
--author=<author> |
指定作者 |
--date=<date> |
指定提交日期 |
基本使用
1. 基本提交
1 2 3 4 5 6 7 8
| $ git commit
$ git commit -m "Add new feature"
$ git commit -m "Add new feature" -m "Detailed description"
|
2. 提交所有更改
1 2 3 4 5 6
| $ git commit -a -m "Update all tracked files"
$ git add -A $ git commit -m "Update all files"
|
3. 修改最后一次提交
1 2 3 4 5 6 7 8 9
| $ git add forgotten-file.txt $ git commit --amend
$ git commit --amend -m "New commit message"
$ git commit --amend --no-edit
|
4. 空提交
1 2
| $ git commit --allow-empty -m "Trigger CI"
|
5. 签名提交
1 2 3 4 5
| $ git commit -S -m "Signed commit"
$ git config --global commit.gpgsign true
|
6. 从文件读取提交信息
1 2 3 4 5
| $ git commit -F commit-message.txt
$ git commit --file=commit-message.txt
|
提交信息规范
基本格式
1 2 3 4 5
| <type>(<scope>): <subject>
<body>
<footer>
|
类型(Type)
feat:新功能
fix:bug 修复
docs:文档更改
style:代码格式(不影响代码运行)
refactor:重构
perf:性能优化
test:测试相关
chore:构建过程或辅助工具的变动
示例
1 2 3 4 5 6 7 8 9 10 11
| $ git commit -m "feat: add user authentication"
$ git commit -m "fix: resolve login error"
$ git commit -m "docs: update README"
$ git commit -m "feat: add user authentication" -m "Implement JWT-based authentication with refresh tokens"
|
实际应用场景
场景1:常规提交
1 2 3 4 5
| $ git add file.txt
$ git commit -m "Add new file"
|
场景2:修改上次提交
1 2 3 4 5
| $ git add forgotten-file.txt
$ git commit --amend --no-edit
|
场景3:提交所有更改
1 2
| $ git commit -a -m "Update all files"
|
场景4:创建 fixup 提交
1 2 3 4 5
| $ git commit --fixup abc1234
$ git rebase -i --autosquash abc1234^
|
场景5:提交时跳过钩子
1 2
| $ git commit --no-verify -m "Quick fix"
|
与其他命令的组合
git commit + git add
1 2 3
| $ git add file.txt $ git commit -m "Add file"
|
git commit + git status
1 2 3
| $ git status $ git commit -m "Commit message"
|
git commit + git log
1 2 3
| $ git commit -m "Add feature" $ git log --oneline -1
|
常见问题和解决方案
问题1:提交信息写错了
解决方案:
1 2
| $ git commit --amend -m "Correct message"
|
问题2:遗漏了文件
解决方案:
1 2 3 4 5
| $ git add forgotten-file.txt
$ git commit --amend --no-edit
|
问题3:想撤销提交
解决方案:
1 2 3 4 5
| $ git reset --soft HEAD~1
$ git reset --hard HEAD~1
|
问题4:提交了错误的文件
解决方案:
1 2 3
| $ git reset HEAD~1 file.txt $ git commit --amend
|
最佳实践
1. 编写清晰的提交信息
1 2 3 4 5 6
| $ git commit -m "feat: add user login functionality"
$ git commit -m "update" $ git commit -m "fix"
|
2. 提交前检查
1 2 3
| $ git status $ git diff --cached
|
3. 小而频繁的提交
1 2 3 4 5 6
| $ git commit -m "feat: add login button" $ git commit -m "feat: add login form"
$ git commit -m "update everything"
|
4. 使用提交信息模板
1 2 3 4 5 6 7 8 9 10 11
| $ git config --global commit.template ~/.gitmessage
$ cat > ~/.gitmessage << EOF # <type>(<scope>): <subject> # # <body> # # <footer> EOF
|
5. 不要使用 –no-verify
1 2 3 4 5
| $ git commit --no-verify
$ git commit
|
总结
git commit 是保存更改到仓库的关键命令:
- 基本用法:
git commit -m "message" 提交暂存区的更改
- 修改提交:
git commit --amend 修改最后一次提交
- 提交所有:
git commit -a 自动暂存并提交所有更改
- 提交信息:遵循规范,编写清晰的提交信息
关键要点:
- ✅ 编写清晰、描述性的提交信息
- ✅ 提交前检查更改
- ✅ 小而频繁的提交
- ✅ 遵循提交信息规范
- ❌ 不要跳过钩子(除非必要)
- ❌ 不要提交不相关的更改