使用git rebase -i来合并多个commit到一个新的commit内git
D:\Git\learngit (master) $ git l * dee2b21 - (HEAD -> master) ddd (1 second ago) | hongda * ff20d90 - ccc (23 seconds ago) | hongda * 931ebca - bbb (55 seconds ago) | hongda * 7af981e - aaa (2 minutes ago) | hongda * 9f015b3 - clean aa.txt (2 minutes ago) | hongda * e4395d8 - eee (80 minutes ago) | hongda
合并最上面的4个commit到9f015b3中,并生成一个新的commitidshell
想要合并1-5条,有两个方法segmentfault
1.从HEAD版本开始往过去数3个版本this
git rebase -i HEAD~5
2.指名要合并的版本以前的版本号spa
git rebase -i e4395d8
请注意ff4cda8这个版本是不参与合并的,能够把它当作一个坐标rest
2.执行了rebase命令以后,会弹出一个窗口,头几行以下:code
pick 9f015b3 clean aa.txt pick 7af981e aaa pick 931ebca bbb pick ff20d90 ccc pick dee2b21 ddd # Rebase e4395d8..dee2b21 onto e4395d8 (5 commands) # # 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 # # 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
2.将pick改成squash或者s,以后保存并关闭文本编辑窗口便可。改完以后文本内容以下:blog
pick 9f015b3 clean aa.txt
s 7af981e aaa
s 931ebca bbb
s ff20d90 ccc
s dee2b21 ddd
3.而后保存退出,Git会压缩提交历史,若是有冲突,须要修改,修改的时候要注意,保留最新的历史,否则咱们的修改就丢弃了。修改之后要记得敲下面的命令:rem
git aa git rebase --continue
若是你想放弃此次压缩的话,执行如下命令:get
git rebase --abort
成功的话,弹出
# This is a combination of 5 commits. # This is the 1st commit message: clean aa.txt # This is the commit message #2: aaa # This is the commit message #3: bbb # This is the commit message #4: ccc # This is the commit message #5: ddd # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Wed Feb 7 15:47:23 2018 +0800 # # interactive rebase in progress; onto e4395d8
修改clean aa.txt 为 clean aa.txt modify,保存并退出
执行结果:
$ git rebase -i e4395d8 [detached HEAD 76efa0a] clean aa.txt modify Date: Wed Feb 7 15:47:23 2018 +0800 1 file changed, 5 insertions(+), 2 deletions(-) Successfully rebased and updated refs/heads/master.
从新查看历史记录:
$ git l * 76efa0a - (HEAD -> master) clean aa.txt modify (2 minutes ago) | hongda * e4395d8 - eee (86 minutes ago) | hongda