git合并多个commit
git合并多个commit
-
查看git log,确认commit
-
git rebase -i xx选择需要合并的commit
-
修改交互界面信息
-
修改注释信息
-
提交
git log -10
#查看前10个commit
git log --oneline
#展示一个commit占一行的简略信息
git rebase -i HEAD~n
#将从HEAD向前n个commit合并
git rebase -i xxxxxxxxxxxx
#将从HEAD到哈希值为xxx的commit合并
#这时出现交互界面,编辑每行commit前面的指令,指定要对这个commit进行的操作
#保存修改
#如果指令有对commit修改信息的指令,则会跳转到修改信息的交互界面
#保存修改
#完成合并,再推送到远端
git push --force
交互编辑的指令解释
-
pick:使用commit
-
fixup:使用commit,丢弃commit信息
-
reword:使用commit,修改commit信息
-
squash:使用commit,将commit信息合入上一个commit
执行命令时的提示:
# 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 commit 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
合并不连续的commit
示例:
$ git log -5 --oneline
8a2a27f (HEAD -> feature_work) fourth commit
aeb5cc2 third commit
a7ef6a8 second commit
516af2d first commit
fc4a3cb (origin/feature_work) start modification
$ git rebase -i fc4a3cb
#进入交互界面后默认显示:
......
pick 516af2d first commit
pick a7ef6a8 second commit
pick aeb5cc2 third commit
pick 8a2a27f fourth commit
......
#这时不止可以修改pick等前缀,也可以手动调整commit的顺序,使不连续的commit也可以合并
#例如可以修改成如下:
pick 516af2d first commit
fixup 8a2a27f fourth commit
pick aeb5cc2 third commit
pick a7ef6a8 second commit
#这代表,第四条commit,保留修改,把commit信息合并到第一条commit内
#然后第二和第三条commit调换位置
#这会导致commit哈希值变化
#执行之后再看commit的日志
$ git log -5 --oneline
8f7e15d (HEAD -> feature_work) second commit
0dd2010 third commit
5458072 first commit
fc4a3cb (origin/feature_work) start modification
aa3aa79 rebase readme
参考:
csdn博客