演示过程中,为便于区分修改结果,未使用commit 版本号,建议各位使用尽量以版本号为准

1.修改历史提交信息

例如:将提交历史 修改提交1改为 develop 修改提交1

修改步骤:

查看 git的提交历史,找到要修改的commit message之前的提交版本号

λ git log --pretty=oneline
37a8fb4fac5e42c59bebb91ebcb7f6a6f857d2a1 :pencil: 修改提交3
b0ac728c9d9a668f4d645d3555a9618f0cdc9699 :pencil: 修改提交2
fb3f99ab476f22fe070a84ffc2399186a1e1a5de :pencil: 修改提交1  # 要修改第一条的提交记录
bb989fb4c2b0fd88e09fb0510bed09d6c70bf350 :wrench: 添加ignore文件
4de7b78dc7f3c1d46fe902f6201f2c6d21c67df0 Initial commit

根据版本号,进行rebase

λ git rebase -i bb989fb  # 这里应该使用需要修改的提交记录前的那个版本号
# pick fb3f99a :pencil: 修改提交1
# pick b0ac728 :pencil: 修改提交2
# pick 37a8fb4 :pencil: 修改提交3
# Rebase bb989fb..37a8fb4 onto bb989fb (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

根据需求编辑提交记录,修改操作和vim操作类似,每个提交 commit 记录前默认都是 pick 操作,可参照如下指令进行修改,每次进入rebase模式,会有提示,无需刻意去记

# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

这里我需要修改commit message,所以将pick改为reword,并修改message。修改后的rebase信息为

# r fb3f99a :pencil: 修改提交1
# r b0ac728 :pencil: 修改提交2
# r 37a8fb4 :pencil: 修改提交3# Rebase bb989fb..37a8fb4 onto bb989fb (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

修改完成后,esc输入x保存退出。依次修改需要修改的message信息,例如第一个commit修改后为:

:pencil: develop 修改提交1# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Aug 18 16:13:24 2021 +0800
#
# interactive rebase in progress; onto bb989fb
# Last command done (1 command done):
#    r fb3f99a :pencil: develop 修改提交1
# Next commands to do (2 remaining commands):
#    r b0ac728 :pencil: develop 修改提交2
#    r 37a8fb4 :pencil: develop 修改提交3
# You are currently editing a commit while rebasing branch 'develop' on 'bb989fb'.
#
# Changes to be committed:
#       modified:   README.md

修改完成后的log信息:

λ git log --pretty=oneline
# 0814ee596f2ca0e0c8a2d497d6759300346a52c4 :pencil: develop 修改提交3
# edb46dabe5db8df49a9aa9d40e502badc11daf4c :pencil: develop 修改提交2
# 15da532e22b39b6ab4d0a7318f3158ddbf033c12 :pencil: develop 修改提交1
# bb989fb4c2b0fd88e09fb0510bed09d6c70bf350 :wrench: 添加ignore文件
# 4de7b78dc7f3c1d46fe902f6201f2c6d21c67df0 Initial commitλ git reflog --pretty=oneline
# 0814ee5 HEAD@{10}: rebase -i (finish): returning to refs/heads/develop
# 0814ee5 HEAD@{11}: rebase -i (reword): :pencil: develop 修改提交3
# 38a0d3c HEAD@{12}: rebase -i (reword): :pencil: 修改提交3
# edb46da HEAD@{13}: rebase -i (reword): :pencil: develop 修改提交2
# a98942d HEAD@{14}: rebase -i (reword): :pencil: 修改提交2
# 15da532 HEAD@{15}: rebase -i (reword): :pencil: develop 修改提交1
# fb3f99a HEAD@{16}: cherry-pick: fast-forward
# bb989fb HEAD@{17}: rebase -i (start): checkout bb989fb

需要注意 修改后有可能需要使用 git push -f 强制推送至远程仓库

2. 将提交进行合并

git log查看当前历史记录

λ git log --pretty=oneline
# c129c21c80ab66b60b90932648c1c798b795ad08 :pencil: master 修改提交3
# 0014c47d51879a88651fbaa0753247a1688fef1e :pencil: master 修改提交2
# f44fa1957f8c240c187948687eabeec8cff3e83f :pencil: develop 修改提交4
# 3d11352e8f69e3483957882f9b2c3ec3dd6b930d :pencil: master 修改提交1
# 0814ee596f2ca0e0c8a2d497d6759300346a52c4 :pencil: develop 修改提交3
# edb46dabe5db8df49a9aa9d40e502badc11daf4c :pencil: develop 修改提交2
# 15da532e22b39b6ab4d0a7318f3158ddbf033c12 :pencil: develop 修改提交1
# bb989fb4c2b0fd88e09fb0510bed09d6c70bf350 :wrench: 添加ignore文件
# 4de7b78dc7f3c1d46fe902f6201f2c6d21c67df0 Initial commit

:pencil: master 修改提交3:pencil: master 修改提交2 进行合并,进入rebase:

λ git rebase -i f44fa1957f
# pick 0014c47 :pencil: master 修改提交2
# pick c129c21 :pencil: master 修改提交3
# Rebase f44fa19..c129c21 onto f44fa19 (2 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

修改完成后的log信息:

# pick 0014c47 :pencil: master 修改提交2
# s c129c21 :pencil: master 修改提交3  # 将 `pick` 改为 `squash(或简写s)`# Rebase f44fa19..c129c21 onto f44fa19 (2 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

修改完成后,esc输入x保存退出。修改合并后的提交信息

# This is a combination of 2 commits.
# The first commit's message is::pencil: master 修改提交2 和 :pencil: master 修改提交3 合并后的提交信息# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Aug 22 00:10:50 2021 +0800
#
# interactive rebase in progress; onto f44fa19
# Last commands done (2 commands done):
#    pick 0014c47 :pencil: master 修改提交2
#    s c129c21 :pencil: master 修改提交3
# No commands remaining.
# You are currently editing a commit while rebasing branch 'develop' on 'f44fa19'.
#
# Changes to be committed:
#       modified:   README.md

输入x保存退出。查看新的log日志:

λ git log --pretty=oneline
# 412612b4e3c96839d8349c268c64b3faf69c84e8  :pencil: master 修改提交2 和 :pencil: master 修改提交3 合并后的提交信
# f44fa1957f8c240c187948687eabeec8cff3e83f :pencil: develop 修改提交4
# 3d11352e8f69e3483957882f9b2c3ec3dd6b930d :pencil: master 修改提交1
# 0814ee596f2ca0e0c8a2d497d6759300346a52c4 :pencil: develop 修改提交3
# edb46dabe5db8df49a9aa9d40e502badc11daf4c :pencil: develop 修改提交2
# 15da532e22b39b6ab4d0a7318f3158ddbf033c12 :pencil: develop 修改提交1
# bb989fb4c2b0fd88e09fb0510bed09d6c70bf350 :wrench: 添加ignore文件
# 4de7b78dc7f3c1d46fe902f6201f2c6d21c67df0 Initial commit

可以看到 master 修改提交2master 修改提交3两个提交已经不存在了,合并产生了一个新提交:pencil: master 修改提交2 和 :pencil: master 修改提交3 合并后的提交信

3. 相关说明

rebase的操作列表中可以看出,可以进行编辑、删除、合并等多个操作。

# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

目前我主要使用reabse进行如下相关操作:

  1. 修改或删除有异议或表述不准确的提交记录(已演示)

  2. 合并多余的提交,避免提交过多导致重要提交信息被淹没(已演示)

需要注意的几个地方:

  1. rebase 时的修改操作与 vim基本一致
  2. rebase时需要选定要修改提交信息之前的提交版本号
  3. rebase应该与远程分支进行比对,切勿被远程分支信息覆盖,或者把远程分支覆盖
  4. 如果出现这类情况,可以尝试用reflog + reset(建议先了解命令机制再尝试),进行恢复

** 表述有误的地方,恳请批评指正,先谢为敬!**

git rebase 的几种用法相关推荐

  1. git rebase的两种用法(最全)

    rebase的两种用法 用法一: 合并当前分支的多个commit记录 1. 找到想要合并的commit, 使用rebase -i 2. 进入Interact交互界面 3.使用s命令 合并到上一个com ...

  2. git commit --amend两种用法

    一.如果已经push到远端服务器,想修改已经提交过的commit信息 1.保存:Ctrl + o; 回车 ;退出:Ctrl + x # git commit --amend 2.重新提交gerrit审 ...

  3. Git之git commit --amend两种用法(五)

    一.如果已经push到远端服务器,想修改已经提交过的commit信息 1.保存:Ctrl + o; 回车 ;退出:Ctrl + x # git commit --amend 2.重新提交gerrit审 ...

  4. git rebase 命令 常用_git命令之 git rebase 常用

    git rebase 命令的功能很强大, 在<git 权威指南>一书里,改变提交(commit)的顺序,书中教了一种很复杂的方法,运用了git reset ... git cherry-p ...

  5. git rebase和git merge的用法

    http://softlab.sdut.edu.cn/blog/subaochen/2016/01/git-rebase%E5%92%8Cgit-merge%E7%9A%84%E7%94%A8%E6% ...

  6. git rebase用法

    开发任务分叉到两个不同分支,又各自提交了更新 整合分支最容易的方法是 merge 命令. 它会把两个分支的最新快照(C3 和 C4)以及二者最近的共同祖先(C2)进行三方合并,合并的结果是生成一个新的 ...

  7. git merge的三种操作merge, squash merge, 和rebase merge

    原链接:https://www.jianshu.com/p/ff1877c5864e git merge的三种操作merge, squash merge, 和rebase merge 举例来说: 假设 ...

  8. git命令之git rebase 的用法

     rebase 假设你现在基于远程分支"origin",创建一个叫"mywork"的分支. $ git checkout -b mywork origin ...

  9. 使用Git rebase合并多条提交记录commit。以及使用 git commit amend本地提交直接合并到远程已有commit的用法

    需求场景一 : 对某个小的功能点进行多次反复的修改提交,且已经提交到远程,导致commit记录过多,太过于杂乱无章,想要精简合并一些提交记录. 场景还原: 比如下图4个git commit记录,log ...

最新文章

  1. java 导出excel 注解_Java基于注解和反射导入导出Excel
  2. STL——vector
  3. 使用FreeRTOS进行性能和运行时分析
  4. 关于H5跳转到小程序和android的方法
  5. 很多人都忽视了账号基建重要性
  6. eclipsevue代码怎么运行_打了多年的单片机调试断点到底应该怎么设置? | 颠覆认知...
  7. seo该如何防止网站被挂***?!
  8. 如何保护 IT 基础设施的安全?谷歌给出了500页的答案
  9. php为什么要创建类,php – 是否有理由为单一功能创建类?
  10. Windows下mysql的基础操作
  11. AOJ2025 Eight Princes
  12. 《敏捷项目管理》知识要点整理
  13. python加粗线宽代码_python-在matplotlib中同时更改线宽和颜色
  14. Domain Driven Design 领域驱动设计
  15. outlook企业邮箱服务器要多少钱,如何用OUTLOOK使用企业邮箱
  16. c 语言编写数字单片机0-9,【学习笔记】单片机的40个经典实验之30:点阵式 LED“0-9”数字显示技术...
  17. 2022 年最值得学习的 10 种编程语言 [更新]
  18. 基于低代码开发平台实现集团战略督办管理系统
  19. IDEA 2021的下载与安装
  20. 使用vue实现日程安排表

热门文章

  1. 智慧街道智慧社区一体化综合管理平台Java商业源码
  2. Linux进程原理及系统调用
  3. vue:实现锚点双向滚动/文章章节联动滚动效果
  4. 腐肉为引,气球为信,负重前行,只为爱你
  5. jqGrid学习笔记(一)
  6. 五点三次平滑滤波在消除随机噪声中的应用以及其Maltab程序
  7. 使用Flexible实现手淘H5页面的终端适配
  8. 深度学习入门笔记(十九):卷积神经网络(二)
  9. pandas的基本功能与常用的数学统计方法
  10. 计算机运行断电 硬盘,电脑突然断电的坏处有哪些?你知道吗?