合并commint

如何合併多個commits

Jan 19th, 2012 | Comments

如果你使用git做版本控管,在一個branch上開發一段時間後,commits看起來又多又雜亂,這個時候你可能會需要整理你的commits,將多個commits合併成1個,而git提供了一個指令可以幫助你完成這件事情:git rebase -i

一個簡單的範例,目標是:將原本git log

a
b1
b2

變成

a
b

也就是將兩個commits,b1和b2,合併成為b。那麼方法和步驟如下:

Step1:$ git rebase -i <不變動的commit的SHA-1>

例如此例就是a的SHA-1 (可用git log查看該commit的版本號)

$ git rebase -i 49687a0a646954afdf3f4dae1f914ea793341ea2

按下enter後會進到編輯模式

Step2:squash

一開始編輯視窗的內容是這樣

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pick 033beb4 b1
pick d426a8a b2

# Rebase 49687a0..d426a8a onto 49687a0
#
# 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
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

分成兩個區塊:第一是rebase interactive要執行的指令,第二是每個指令的簡單說明(‘#’後的是注解)。在這個例子裡面,我們至少要搞懂以下兩個:

pick = use commit
squash = use commit, but meld into previous commit

也就是說,pick是會執行該commit,而squash會把這個版本的commit合併到前一個commit。所以我們要將它改成:

1
2
pick 033beb4 b1
squash d426a8a b2

也就是將d426a8a這個版本的commit併到版本號033beb4的commit,對應我們的例子,就是將b2合併到b1這個commit。

在編輯完成後按下esc,打上:wq 儲存離開。

Step3:接著輸入新的commit message

剛進來這個畫面,它會告訴你原本兩個commit messages分別是b1和b2,現在你要輸入新的commit message,也就是b,建議可以把原本的訊息註解掉。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值