【Git】修改提交信息(单次、批量)

文章目录

  • [修改最近一次 commit 的提交信息](#修改最近一次 commit 的提交信息)
  • [修改某次 commit 的提交信息](#修改某次 commit 的提交信息)
  • 方法总结

修改最近一次 commit 的提交信息

bash 复制代码
 git commit --amend -m "new message"

修改某次 commit 的提交信息

bash 复制代码
git log --oneline
运行结果如下

2f80f1b commit 4
9ee990a 第三次提交
40f2f03 commit 2
8ec9f83 first commit

实战:将 9ee990a 的提交信息改为:commit 3

bash 复制代码
# 待修改提交的前一个提交的修订号
git rebase -i 40f2f03 
运行结果如下

pick 9ee990a commit 第三次提交
pick 2f80f1b commit 4

进入编辑模式(按 i),将第一行的 pick 改为 e(方向键移动光标),保存退出(按 ESC,输入:wq,回车)

此时 git 会有如下提示

bash 复制代码
Stopped at 9ee990a...  第三次提交
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

由提示得知,先运行第一个命令修改提交信息

bash 复制代码
git commit --amend -m "commit 3"

修改好后再运行第二个命令保存

bash 复制代码
git rebase --continue

若要批量修改,那就将多个 pick 改为 e,然后依次按顺序重复以上两个命令

方法总结

修改最近一次提交信息,可以用 git commit --amend,也可以用git rebase -i,推荐使用前者,更方便

修改某次提交信息,用git rebase -i,pick 改为 e,再通过git commit --amendgit rebase --continue完成后续操作

批量修改提交信息,用git rebase -i,多个 pick 改为 e,再通过git commit --amendgit rebase --continue完成多个后续操作

还可用git filter-branch -f --env-filter编写脚本完成批量修改

相关推荐
恋喵大鲤鱼15 小时前
git mv
git·git mv
Patrick_Wilson19 小时前
Git Worktree 原理详解:从 objects / refs 看懂多分支并行与多 Agent 协作
git·面试·ai编程
yaoxiaoganggang19 小时前
克隆 Superpowers 的规则库到你的本地(或者直接作为 Git Submodule)
人工智能·经验分享·git·ai编程
Python私教1 天前
用 Claude Code 做大型重构不翻车:分批+Git 兜底+验证闭环的实战流程(2026)
git·重构·ai编程·代码重构·工程实践·claude code
Shawn Dev1 天前
团队协作中的 Git Tag 最佳实践:从入门到精通
大数据·git·elasticsearch
独隅1 天前
Git/GitHub/GitLab/Gitee 核心对比指南
git·gitlab·github
佛山个人技术开发1 天前
GitCode个人技术开发者总结完整使用指南
windows·git·svn·github·gitcode
江华森1 天前
基于 Git 的自动集成交付(Git-Driven CI/CD)实战
git·ci/cd
Dontla1 天前
.gitkeep文件作用(让Git追踪空目录,使该目录能被纳入版本控制)!.gitkeep
大数据·git·elasticsearch
shandianchengzi1 天前
【记录】VSCode|Windows 下 VS Code 配置 Git Bash 为默认终端完整教程
windows·git·vscode·bash