Git多次提交记录合并
Git多次提交记录合并
开发过程中,使用Git有时候会遇到多次零碎的小提交,想要合并成一个提交记录,这里记录一下操作。
讲解部分是通过从零开始搭个仓库做试验,回顾一下用法;方法部分则是总结用法。
讲解
第一步,先建新一个目录并初始化为空的Git仓库
mkdir merge
cd merge
git init
第二步,新建4个文件做4次提交
vim a.txt
## 输入 first commit
vim b.txt
## 输入 second commit
vim c.txt
## 输入 third commit
vim d.txt
## 输入 forth commit
git add a.txt
git commit -m "first commit"
git add b.txt
git commit -m "second commit"
git add c.txt
git commit -m "thrid commit"
git add d.txt
git commit -m "forth commit"
第三步, 进行多次提交记录的合并
先查看一下近三次的提交记录
git log --pretty=format:"%h %s" HEAD~3..HEA
bf0b37 forth commit
18a5c11 third commit
19d685a second commit
执行交互式变基操作
git rebase -i HEAD~3
执行上述命令后会出现对应的交互操作提示
pick 19d685a second commit
pick 18a5c11 third commit
pick 3bf0b37 forth commit
# Rebase 205fd2c..3bf0b37 onto 205fd2c (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 <commit> = like "squash", but discard this commit's log message
# 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 was
# . specified). Use -c <commit> to reword the commit message.
#
# 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
为了将三次提交合并为一次,需要对后两次提交记录使用squash
命令
pick 19d685a second commit
s 18a5c11 third commit
s 3bf0b37 forth commit
接着对vi编辑器进行保存后退出,Git会出现合并后的commit提示信息输入
# This is a combination of 3 commits.
# This is the 1st commit message:
second commit
# This is the commit message #2:
third commit
# This is the commit message #3:
forth commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sun Sep 20 21:49:54 2020 +0800
#
# interactive rebase in progress; onto 205fd2c
# Last commands done (3 commands done):
# squash 18a5c11 third commit
# squash 3bf0b37 forth commit
# No commands remaining.
# You are currently rebasing branch 'master' on '205fd2c'.
#
# Changes to be committed:
# new file: b.txt
# new file: c.txt
# new file: d.txt
将上述的提交信息改成合并后想要的信息即可
merge commit from second, third, forth
保存退出,看到Git已经帮我们的修改提交成功了
[detached HEAD 25d7655] merge commit from second, third, forth
Date: Sun Sep 20 21:49:54 2020 +0800
3 files changed, 3 insertions(+)
create mode 100644 b.txt
create mode 100644 c.txt
create mode 100644 d.txt
再次查看提交记录,变成了两次提交
git log --pretty=oneline
25d7655d46ea6585c1278022b04a6d711c95d50f (HEAD -> master) merge commit from second, third, forth
205fd2c637269b348301070ccb29d4121c2d8b02 first commit
至此,合并3次提交为一次就成功了。
方法
合并多次Git提交的方法是通过交互式变基命令实现的,通过变基将提交历史进行修改。
# n为需要处理的提交次数
git rebase -i HEAD~n
然后交互式变基的操作中对需要合并的提交记录选择squash
命令即可。
使用后对应修改合并后的提交信息保存。
注意
- 如果本地的Git提交记录已经推送到远程分支,则不要进行变基操作,否则可能会给其他开发者造成困扰。