使用 git rebase 合并多个 commit

这篇具有很好参考价值的文章主要介绍了使用 git rebase 合并多个 commit。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

首先我们查看一下当前提交历史:

atreus-MBP:code (test) $ git log -4 --oneline
da3ba01 (HEAD -> test) 3
9d2725f 2
44f23cb 1
61e7d87 (origin/test) merge: Merge branch 'test' of https://gitee.com/atreus1125/code into test

我们通过 git rebase -i 61e7d8744f23cb9d2725fda3ba01 这三个提交合并,这里的 61e7d87待合并的提交区间的前一个提交的哈希值

atreus-MBP:code (test) $ git rebase -i 61e7d87
[detached HEAD 92d933c] 1 & 2 & 3 2
 Date: Wed Jan 24 12:06:50 2024 +0800
 1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/test.

执行之后会进入到 vim 编辑器中,每一行代表一个 todo 项。我们这里需要 pick 第一个提交并将后面两个提交向前压缩。

修改前:

pick 44f23cb 1
pick 9d2725f 2
pick da3ba01 3

# Rebase 61e7d87..da3ba01 onto 61e7d87 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
#         create a merge commit using the original merge commit's
#         message (or the oneline, if no original merge commit was
#         specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#

修改后:

pick 44f23cb 1
squash 9d2725f 2
squash da3ba01 3

# Rebase 61e7d87..da3ba01 onto 61e7d87 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
#         create a merge commit using the original merge commit's
#         message (or the oneline, if no original merge commit was
#         specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#

:wq 保存后会自动进入注释编辑,这里需要修改我们想要保留的最终 commit message。

修改前:

# This is the commit message #2:

2

# This is the commit message #3:

3

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Jan 24 12:06:50 2024 +0800
#
# interactive rebase in progress; onto 61e7d87
# Last commands done (3 commands done):
#    squash 9d2725f 2
#    squash da3ba01 3
# No commands remaining.t/COMMIT_EDITMSG" 28L, 610B
# You are currently rebasing branch 'test' on '61e7d87'.
#
# Changes to be committed:
#       modified:   src/main/java/file
#

修改后:

1 & 2 & 3

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Jan 24 12:06:50 2024 +0800
#
# interactive rebase in progress; onto 61e7d87
# Last commands done (3 commands done):
#    squash 9d2725f 2
#    squash da3ba01 3
# No commands remaining.t/COMMIT_EDITMSG" 28L, 610B
# You are currently rebasing branch 'test' on '61e7d87'.
#
# Changes to be committed:
#       modified:   src/main/java/file
#

保存后即可完成 rebase,此时 git 日志如下:文章来源地址https://www.toymoban.com/news/detail-824809.html

atreus-MBP:code (test) $ git log -2 --oneline
92d933c (HEAD -> test) 1 & 2 & 3
61e7d87 (origin/test) merge: Merge branch 'test' of https://gitee.com/atreus1125/code into test

到了这里,关于使用 git rebase 合并多个 commit的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【git 使用】使用 git rebase -i 修改任意的提交信息/合并多个提交

    修改最近一次的提交信息的方法有很多,可以参考这篇文章,但是对于之前的提交信息进行修改只能使用 rebase。 假设我们想修改下面这个提交信息,想把【登录】改成【退出登录】步骤如下 运行 git rebase -i head~3 打开了一个文本编辑器 -i  【interactive】参数表示进行交互式

    2024年02月21日
    浏览(29)
  • git rebase 合并多个提交

    开发过程中,本地通常会有无数次 commit ,可以合并相同功能的多个 commit,以保持历史的简洁。 命令使用 说明: -i(–interactive):弹出交互式的界面进行编辑合并 [commitid]:要合并多个版本之前的版本号,注意:[commitid] 本身不参与合并 例如,如下例子中你想合并前 5 个

    2024年02月07日
    浏览(27)
  • 【git】多个git commit合并

    提交MR之前存在多个commit信息,需要合并为一个。 比如 存在如下多个commit信息。 第二个方法主要通过rebase方法重置 参考: https://blog.csdn.net/Spade_/article/details/108698036

    2024年02月05日
    浏览(28)
  • git如何合并多个commit

    1. git rebase -i HEAD~n n:表示要合并的commit个数 例如:git rebase -i HEAD~6,得到如下界面 2. 将要合并的提交pick改为s(squash),将下面5条commit压缩到第一条 3. 修改完毕后,按esc退出编辑,按:q放弃保存并退出,按:wq保存并退出,得到如下界面 如果遇到如下情况,选择直接编辑即可,

    2024年02月14日
    浏览(22)
  • 【Git】在 IDEA 中合并多个 commit 为一个

    分两种情况: 一种是本地提交还没推到远程,这种好处理 另一种是已经提交到远程分支,这个略麻烦 我想把选中的 4 个commit合并为 1 个 选中要合并的 commit 的前一个,右键选择 reset。在弹窗中选默认的就好,然后有冲突就解决冲突 reset 后,在 local change 页面可以看到前几个

    2024年01月20日
    浏览(33)
  • Git代码合并之使用 rebase 整理提交历史

    ​以面试题为目标来进行学习,不定期推出前端高频面试题及其解析,题无简难,重在积累,欢迎wx关注 西西星球 ! Git 中整合来自不同分支的修改有两种方式: git merge 和 git rebase 。本文主要介绍 rebase 的3种使用场景: 场景1: 使用 rebase 合并分支–整合分叉的提交历史 使用

    2023年04月15日
    浏览(30)
  • Git分支的合并策略有哪些?Merge和Rebase有什么区别?关于Merge和Rebase的使用建议

    参考:《Git 权威指南》、《Git团队协作》、快手git管理 (1)工作区域 首先来介绍介绍下Git的工作区域,分为 工作区 、 暂存区 和 仓库区 ,每个区域的转换关系如上图所示。 工作区(workspace) :就是我们平时本地存放项目代码的地方; 暂存区(index/stage) :用于临时存放

    2024年02月04日
    浏览(47)
  • git rebase 合并提交与避免分叉合并

    本文让你熟练使用 rebase,学会以下两种操作,从此拒绝杂乱无章的 git 提交。 你可能出现过对同一处代码进行多次处理的场景。这会导致如下提交记录: 其实,中间的对 b 的3次提交完全可以合并成一次 commit,这个时候 rebase 就很有用了。 step1. 找到想要合并的 commit, 使用 re

    2024年02月15日
    浏览(37)
  • git rebase 合并提交

    git log --oneline 查看当前提交记录 git rebase -i HEAD~2 选择最后提交的2条记录进行合并 进入编辑界面,将c865404的 pick 改为 f , 表示向前合并也就是向cc5a54合并 编辑完之后 :wq 保存并退出 git rebase --continue git push --force origin feature/v1.2 推送到仓库 git rebase --abort 取消变基

    2024年02月16日
    浏览(28)
  • 【随笔】Git 高级篇 -- 提交的技巧(上) rebase & commit --amend(十八)

    💌 所属专栏:【Git】 😀 作  者:我是夜阑的狗🐶 🚀 个人简介:一个正在努力学技术的CV工程师,专注基础和实战分享 ,欢迎咨询! 💖 欢迎大家:这里是CSDN,我总结知识的地方,喜欢的话请三连,有问题请私信 😘 😘 😘 您的点赞、关注、收藏、评论,是对我最大

    2024年04月22日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包