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 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
承渊政道1 天前
Linux系统学习【Linux系统的进度条实现、版本控制器git和调试器gdb介绍】
linux·开发语言·笔记·git·学习·gitee
Doro再努力1 天前
【Linux操作系统12】Git版本控制与GDB调试:从入门到实践
linux·运维·服务器·git·vim
摇滚侠1 天前
MAC IDEA GIT 提交区显示了几个不存在的目录
git·idea
城东1 天前
Git使用[远程仓库远端的head比本地和提交的head旧,其他人拉不到最新代码]
git·head·远程仓库远端·比本地和提交的head旧·其他人拉不到最新代码
何中应2 天前
使用SSH地址拉取远程仓库代码报下面的错误
git
何中应2 天前
Git本地仓库命令补充
git
sun0077002 天前
执行repo sync -c -d -j4以后,提交未git push的代码看不到了。要怎么恢复?
git
胖虎12 天前
Git 一个本地仓库同时推送到两个远程仓库(详细教程)
git·多远程仓库·双远程仓库·git双远程·git备份
春日见2 天前
如何创建一个PR
运维·开发语言·windows·git·docker·容器