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都合并成一个:

相关推荐
爱学英语的程序员1 天前
Git 提交 LF will be replaced by CRLF the next time Git touches it 报错
git
qq_339191141 天前
服务器git pull每次都要输入密码,linux 设置git登录,linux设置git只输入一次账户密码
git
一颗小行星!1 天前
快速理解 Git submodule
git
A-Jie-Y1 天前
Git基础-核心概念与常用命令
git
夜珀1 天前
Git基础修炼手册:在AtomGit上玩转版本控制
git
golang学习记1 天前
Zed IDE官宣新招:Git Graph 正式支持!
ide·git
要记得喝水1 天前
适用于 Git Bash 的脚本,批量提交和推送多个仓库的修改
git·elasticsearch·bash
AI_Claude_code1 天前
专栏导论:开源项目贡献的价值与Git工作流全景图
git·开源
never forget shyang1 天前
CCS20.2.0使用教程
c语言·git·单片机
lifewange2 天前
常用的Git命令有哪些?
git