使用git rebase -i来合并多个commit到一个新的commit内
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中,并生成一个新的commitid
想要合并1-5条,有两个方法
1.从HEAD版本开始往过去数3个版本
git rebase -i HEAD~5
2.指名要合并的版本之前的版本号
git rebase -i e4395d8
请注意ff4cda8这个版本是不参与合并的,可以把它当做一个坐标
2.执行了rebase命令之后,会弹出一个窗口,头几行如下:
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,之后保存并关闭文本编辑窗口即可。改完之后文本内容如下:
pick 9f015b3 clean aa.txt
s 7af981e aaa
s 931ebca bbb
s ff20d90 ccc
s dee2b21 ddd
3.然后保存退出,Git会压缩提交历史,如果有冲突,需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了。修改以后要记得敲下面的命令:
git aa git rebase --continue
如果你想放弃这次压缩的话,执行以下命令:
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
所有评论(0)