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

相关推荐
゛凌乱的记忆づ14 小时前
好用的md文件编辑器——TyporaS与Typora
笔记·git·typora·markdown
এ慕ོ冬℘゜17 小时前
Git 基础规范:小步提交、提交信息规范与常用命令
git·svn
忘了ʷºᵇₐ18 小时前
Git学习笔记
笔记·git·学习
BIBI20491 天前
换 Git 托管平台怎么做?基于 TortoiseGit 的跨平台迁移教程
windows·git·tortoisegit·问题解决·版本控制·最佳实践
echome8882 天前
Git 日常救急:撤销提交、找回误删文件和解决冲突
git·版本控制
赵民勇2 天前
Git命令详解
git
不老刘3 天前
Antigravity 的 New Worktree 模式:给 AI 代理一个安全的“平行工位”
人工智能·git·ai编程·antigravity
光谱SpectrAI3 天前
Windows 也中招:Git Bash 和 Node 仍信任泄露的 eMudhra 证书
git
YIN_尹3 天前
【git原理与使用】git初识与安装
大数据·git·elasticsearch
Leo_Mr3 天前
Vue3 项目配置 Git 提交规范(企业通用方案:husky + commitlint + cz-git + lint-staged)
git