Git 合併多個commit

使用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

 

http://www.javashuo.com/article/p-slqufshl-kn.html

相關文章
相關標籤/搜索