本文已参与「新人创作礼」活动,一起开启掘金创作之路。git 合并多个commit

148 阅读1分钟

进入分支,使用git log 查看提交日志,退出查看日志直接输入Q 即可。

image.png

选择要合并多个commit前的那个commit id. 选中要 d2422a9c9b20fcf56c6fcc53716f4b98a8b87f2b ,按快捷键 Ctrl + Insert,复制commit id; shift + Inseert 是粘贴。

SourceTree可以按如下方式复制commit id
image.png

执行: git rebase -i d2422a9c9b20fcf56c6fcc53716f4b98a8b87f2b
然后再输入: i 进入编辑模式。在需要被合并的分支前输入f或s即可,f和s的定义请看下面。\

第一列是rebase具体执行的操作,其中操作可以选择,其中含义如下: 一般选择fixup 和 squash,直接输入f或s即可。

  • 选择pick操作,git会应用这个补丁,以同样的提交信息(commit message)保存提交
  • 选择reword操作,git会应用这个补丁,但需要重新编辑提交信息
  • 选择edit操作,git会应用这个补丁,但会因为amending而终止
  • 选择squash操作,git会应用这个补丁,但会与之前的提交合并
  • 选择fixup操作,git会应用这个补丁,但会丢掉提交日志
  • 选择exec操作,git会在shell中运行这个命令 image.png

image.png

保存并退出 先按 Esc 键,然后输入: :wq image.png

按下 Esc 键进入命令模式,键入 :q!  回车后放弃修改并退出

image.png 根据需求修改,一般使用 f 或 s即楞。 git rebase -I [commit id]

# 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