git使用技巧 - commit信息提交

一、参考资料

二、commit message

Note:"写 commit message 像写邮件标题,让人一眼看懂做了什么。"

commit 格式

bash 复制代码
<type>(<scope>): <subject>

<body>  (可选)
<footer>(可选)

简单示例:

bash 复制代码
git commit -m "[feat](camera): add recording status API"

git commit -m "[fix](shm): resolve null pointer when recordStatus not found"

git commit -m "[docs](readme): update build instructions"

type 类型

类型 说明
feat 新功能
fix Bug 修复
docs 文档变更
style 代码格式(无逻辑变动)
refactor 重构
test 测试相关
chore 杂项(构建、依赖、配置)

多行commit(交互式)

方式一

bash 复制代码
# 多个-m标志来创建"多行提交"
git commit -m "[fix]: (1) xxx." -m "[fix]: (2) xxx."

# 双引号"",换行
git commit -m "[fix]: (1) xxx.
[fix]: (2) xxx.
"

方式二

bash 复制代码
git commit

会打开默认编辑器(如 vim),按下面格式写:

bash 复制代码
[feat](recording): add duration_time field to JSON response

- calculate duration from shared memory timestamp
- return 0 when status is 'close'

Closes #123

保存退出即可。

三、修改commit信息

Nano 编辑器中,怎样保存和退出-CSDN博客

git高级浅入之当我们需要修改某次commit信息 - 林璡 - 博客园

1. 修改最近一次commit(合并最近一次commit)

修改commit:

bash 复制代码
git commit --amend

弹出nano编辑器,修改完毕后,按【ctrl+x】退出,按住大写字母【Y】,按【Enter】回车,保存并退出。

2. 修改特定历史commit

step1:启动交互式rebase。

bash 复制代码
git rebase -i HEAD~n

将 n 替换为需要回溯的提交数。例如,HEAD~3 表示最近 3 次提交。

step2:选择要修改的commit。

在打开的列表中,将目标commit前面的 pick 改为 edit,按【ctrl+x】退出,按住大写字母【Y】,按【Enter】回车,保存并退出。

step3:修改提交。

编辑commit信息,按【ctrl+x】退出,按住大写字母【Y】,按【Enter】回车,保存并退出。

bash 复制代码
git commit --amend

step4:继续rebase。

bash 复制代码
git rebase --continue

3. 合并多个commit(rebase方式)

[Git] 两种方法合并多个commit为一个-CSDN博客

rebase时合并多个commit为一个commit,应该如何处理? - 知乎

查看log

如果想合并最近5个commit为1个(旧的在前,新的在后)。

启动交互式rebase

bash 复制代码
git rebase -i HEAD~5
# 或者
git rebase -i 51efaef517abdbf674478de6073c12239d78a56a # 倒数第五个的commid id

在打开的列表中,将前4个commit前面的pick修改为 fixup,保留第5个 pick。按【ctrl+x】退出,按住大写字母【Y】,按【Enter】回车,保存并退出。

  • pick:使用commit。
  • reword:使用commit,修改commit信息。
  • squash:使用commit,将commit信息合入上一个commit。
  • fixup:使用commit,丢弃commit信息。

操作完之后,发现commit都合并成一个:

相关推荐
jiayong234 小时前
Git 核心概念:Tag 与 Branch 的本质区别
git
Serene_Dream8 小时前
git 合并冲突的分支
git
我是一只puppy8 小时前
使用AI进行代码审查
javascript·人工智能·git·安全·源代码管理
玄同7659 小时前
Git常用命令指南
大数据·git·elasticsearch·gitee·github·团队开发·远程工作
十步杀一人_千里不留行12 小时前
Git提交前ESLint校验实践(Husky + lint-staged)
git·github
hh随便起个名15 小时前
适合小白的git的基础使用方法
git
我会一直在的15 小时前
Devps持续集成
git·ci/cd
CoderJia程序员甲16 小时前
GitHub 热榜项目 - 日榜(2026-02-08)
git·ai·开源·llm·github
Serene_Dream18 小时前
git 常用命令
git
jiayong2318 小时前
Detached HEAD 状态详解
git