当前位置:   article > 正文

使用 git rebase 合并多个 commit_合并commit记录

合并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
  • 1
  • 2
  • 3
  • 4
  • 5

我们通过 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.
  • 1
  • 2
  • 3
  • 4
  • 5

执行之后会进入到 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
#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

修改后:

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
#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

: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
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

修改后:

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
#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

保存后即可完成 rebase,此时 git 日志如下:

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
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/584504
推荐阅读
相关标签
  

闽ICP备14008679号