将多个较旧的提交合并为一个

我目前有一个如下所示的分支(A最近的提交和K最旧的,几个月前)

A-B-C-D-E-F-G-H-J-K   (branch-alpha)

我想做的是替换提交D-GZ以便生成的分支看起来像这样

A-B-C-Z-H-J-K   (branch-alpha)

我能找到的所有例子都是指结合最近的提交,但我想保留最近的 3 次提交。我想将第 4、5、6、7 号提交合二为一。我怎样才能做到这一点?

回答

假设A - K是最后 10 次提交,您可以运行命令

git rebase -i HEAD~10

这将打开“交互式”=>-i命令提示符编辑器(包含最后 10 次提交),如下所示:

pick a2260e9 A
pick ff5e839 B
pick 965d9ea C
pick b09f2ae D
pick d1c9f15 E
pick 93309a9 F
pick e17d8fd G
pick d10491b H
pick 110c55f J
pick b57bc16 K

# Rebase ae65653..b57bc16 onto ae65653
#
# 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
#
# 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

然后,您将以下几行从pick更改s为“壁球”

pick a2260e9 A
pick ff5e839 B
pick 965d9ea C
pick b09f2ae D
s d1c9f15 E
s 93309a9 F
s e17d8fd G
pick d10491b H
pick 110c55f J
pick b57bc16 K

保存文件。在下一个屏幕上,它会询问您有关提交消息的信息。#在尚未得到 a 的每一行上添加 a #。这将被视为评论。添加一行只有一个Z,然后将是压扁提交的提交消息

最终结果如下:

a2260e9 A
ff5e839 B
965d9ea C
38e9fab Z
553c8c7 H
23acc30 J
143561a K


以上是将多个较旧的提交合并为一个的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>