0


【GIT操作】git如何合并多个commit

【GIT操作】如何合并多个commit

场景

在单个分支中有着多次commit,并push到了远程分支上。假设分支的commit如下图,一共有5次commit。

#mermaid-svg-IjwKybWlOuc5GrQp {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-IjwKybWlOuc5GrQp .error-icon{fill:#552222;}#mermaid-svg-IjwKybWlOuc5GrQp .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IjwKybWlOuc5GrQp .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-IjwKybWlOuc5GrQp .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IjwKybWlOuc5GrQp .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IjwKybWlOuc5GrQp .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IjwKybWlOuc5GrQp .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IjwKybWlOuc5GrQp .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IjwKybWlOuc5GrQp .marker.cross{stroke:#333333;}#mermaid-svg-IjwKybWlOuc5GrQp svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IjwKybWlOuc5GrQp .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-IjwKybWlOuc5GrQp .cluster-label text{fill:#333;}#mermaid-svg-IjwKybWlOuc5GrQp .cluster-label span{color:#333;}#mermaid-svg-IjwKybWlOuc5GrQp .label text,#mermaid-svg-IjwKybWlOuc5GrQp span{fill:#333;color:#333;}#mermaid-svg-IjwKybWlOuc5GrQp .node rect,#mermaid-svg-IjwKybWlOuc5GrQp .node circle,#mermaid-svg-IjwKybWlOuc5GrQp .node ellipse,#mermaid-svg-IjwKybWlOuc5GrQp .node polygon,#mermaid-svg-IjwKybWlOuc5GrQp .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-IjwKybWlOuc5GrQp .node .label{text-align:center;}#mermaid-svg-IjwKybWlOuc5GrQp .node.clickable{cursor:pointer;}#mermaid-svg-IjwKybWlOuc5GrQp .arrowheadPath{fill:#333333;}#mermaid-svg-IjwKybWlOuc5GrQp .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-IjwKybWlOuc5GrQp .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-IjwKybWlOuc5GrQp .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-IjwKybWlOuc5GrQp .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-IjwKybWlOuc5GrQp .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-IjwKybWlOuc5GrQp .cluster text{fill:#333;}#mermaid-svg-IjwKybWlOuc5GrQp .cluster span{color:#333;}#mermaid-svg-IjwKybWlOuc5GrQp div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-IjwKybWlOuc5GrQp :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
1

2

3

4

5

计划将第二次commit至第五次commit合并成一个commit。合并效果,如下图。

#mermaid-svg-Nq8jxxt5MD8HCAxY {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-Nq8jxxt5MD8HCAxY .error-icon{fill:#552222;}#mermaid-svg-Nq8jxxt5MD8HCAxY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Nq8jxxt5MD8HCAxY .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-Nq8jxxt5MD8HCAxY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Nq8jxxt5MD8HCAxY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Nq8jxxt5MD8HCAxY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Nq8jxxt5MD8HCAxY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Nq8jxxt5MD8HCAxY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Nq8jxxt5MD8HCAxY .marker.cross{stroke:#333333;}#mermaid-svg-Nq8jxxt5MD8HCAxY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Nq8jxxt5MD8HCAxY .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Nq8jxxt5MD8HCAxY .cluster-label text{fill:#333;}#mermaid-svg-Nq8jxxt5MD8HCAxY .cluster-label span{color:#333;}#mermaid-svg-Nq8jxxt5MD8HCAxY .label text,#mermaid-svg-Nq8jxxt5MD8HCAxY span{fill:#333;color:#333;}#mermaid-svg-Nq8jxxt5MD8HCAxY .node rect,#mermaid-svg-Nq8jxxt5MD8HCAxY .node circle,#mermaid-svg-Nq8jxxt5MD8HCAxY .node ellipse,#mermaid-svg-Nq8jxxt5MD8HCAxY .node polygon,#mermaid-svg-Nq8jxxt5MD8HCAxY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Nq8jxxt5MD8HCAxY .node .label{text-align:center;}#mermaid-svg-Nq8jxxt5MD8HCAxY .node.clickable{cursor:pointer;}#mermaid-svg-Nq8jxxt5MD8HCAxY .arrowheadPath{fill:#333333;}#mermaid-svg-Nq8jxxt5MD8HCAxY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Nq8jxxt5MD8HCAxY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Nq8jxxt5MD8HCAxY .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-Nq8jxxt5MD8HCAxY .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-Nq8jxxt5MD8HCAxY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Nq8jxxt5MD8HCAxY .cluster text{fill:#333;}#mermaid-svg-Nq8jxxt5MD8HCAxY .cluster span{color:#333;}#mermaid-svg-Nq8jxxt5MD8HCAxY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Nq8jxxt5MD8HCAxY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
1

5'

操作步骤

通过git bash工具交互式操作。

  1. 查询commit的commitIDgit log --oneline``````--oneline参数,代表将每个commit压缩成一行,只显示commitIDcommit message的第一行。d5ace53 第5次commit25d5ae5 第4次commit73c98f9 第3次commita50f132 第2次commitd5d1335 第1次commit
  2. 变基到第一次commit,开启交互式操作。git rebase -i d5d1335``````-i参数,代表展开交互界面pick a50f132 第2次commitpick 73c98f9 第3次commitpick 25d5ae5 第4次commitpick d5ace53 第5次commit# Rebase d5d1335..d5ace53 onto d5d1335 (4 command)## 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按insert键,进入编辑模式,将3,4,5commit的pick修改成ssquashpick a50f132 第2次commits 73c98f9 第3次commits 25d5ae5 第4次commits d5ace53 第5次commit# Rebase d5d1335..d5ace53 onto d5d1335 (4 command)## 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保存,展开修改message的界面,类似rebase的界面。# This is a combination of 4 commits.# This is the 1st commit message:第2次commit# This is the commit message #2:第3次commit# This is the commit message #3:第4次commit# This is the commit message #4:第5次commit# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date: Fri Dec 29 17:22:02 2023 +0800## interactive rebase in progress; onto d5d1335# Last commands done (4 commands done):# pick a50f132 第2次commit# s 73c98f9 第3次commit# s 25d5ae5 第4次commit# s d5ace53 第5次commit# No commands remaining.# You are currently rebasing branch 'Dev_Br20231207-mergecommit' on 'd5d1335'.## Changes to be committed:修改后保存。# This is a combination of 4 commits.# This is the 1st commit message:合并commit2至commit5# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date: Fri Dec 29 17:22:02 2023 +0800## interactive rebase in progress; onto d5d1335# Last commands done (2 commands done):# pick a50f132 第2次commit# s 73c98f9 第3次commit# s 25d5ae5 第4次commit# s d5ace53 第5次commit# No commands remaining.# You are currently rebasing branch 'Dev_Br20231207-mergecommit' on 'd5d1335'.## Changes to be committed:保存后会回到命令行界面。$ git rebase -i d5d1335[detached HEAD fce4ac1]1.测试1 1.测试2 Date: Fri Dec 2917:22:02 2023 +0800 1file changed, 1 insertion(+), 1 deletion(-)Successfully rebased and updated refs/heads/Dev_Br20231207-mergecommit.
  3. 强制推送到远程分支。git push -f可能需要输出账号密码。

注意点

  1. 这种方式只能支持单个分支中的普通提交点。
  2. 不支持多分支合并无法操作。
标签: git

本文转载自: https://blog.csdn.net/weixin_43820556/article/details/135295800
版权归原作者 顧棟 所有, 如有侵权,请联系我们删除。

“【GIT操作】git如何合并多个commit”的评论:

还没有评论