git rebase 的几种用法

演示过程中,为便于区分修改结果,未使用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(建议先了解命令机制再尝试),进行恢复

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值