cherry-pick
假如現(xiàn)在有兩個分支v1.0阳懂,v1.1豺谈。
v1.0有如下commit:
commit 4d3b38f3e6b9f49776f6e2d2861f0425e10df8d6 (HEAD -> v1.0)
Author: bin <bininhere@163.com>
Date: Tue Mar 19 10:33:43 2019 +0800
feature5
commit 65ad383c977acd6c7e7bed486bbf3631851a9eda
Author: bin <bininhere@163.com>
Date: Tue Mar 19 10:30:44 2019 +0800
feature4
commit a2a438f2652166f13a6a2aa36f447968fff3b15d
Author: bin <bininhere@163.com>
Date: Tue Mar 19 10:30:09 2019 +0800
feature3
現(xiàn)在v1.1需要合并feature4的功能,但不能合并feature3钮孵,feature5戈泼,怎么辦婿禽?
把代碼復(fù)制過來嗎?不大猛!用cherry-pick
git cherry-pick 65ad383c977acd6c7e
如果文件有沖突扭倾,cherry-pick 會中斷,
你解決沖突后挽绩,使用git add添加沖突文件膛壹,使用git cherry-pick --continue完成cherry-pick操作。
或者使用git cherry-pick --abort中斷操作。
如果你cherry-pick的是別人分支的commit模聋,可能會遇到錯誤fatal: bad object ...
肩民,那是因?yàn)間it cherry-pick是本地特性,本地要有這個commit才可以被git cherry-pick撬槽。如果沒有這個commit id此改,就會出現(xiàn)這個錯誤趾撵。
git rebase
之前都不太在意 git rebase的使用侄柔,直到看了這篇文章
徹底搞懂 git rebase,規(guī)范項(xiàng)目的commit
想想也是的占调,如果git log充斥著大量如下commit:
fixbug
fixbug
fixbug
add log
add log
add log
add log
除了文章中提到的
1.不利于代碼 review
2.不利于代碼回滾
也不利于自己回顧跟蹤代碼暂题。
所以可以使用rebase合并commit。
我把上面v1.0所有commit合并為一個
git rebase -i a2a438f2652 4d3b38f3e
其中-i的意思是--interactive究珊,即彈出交互式的界面讓用戶編輯完成合并操作薪者。
這時會彈出編輯頁面
r a2a438f feature3
# Rebase 1822165..46c1e55 onto 1822165 (3 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.
上面是指令編輯,下面注釋是指令說明:
pick:保留該commit(縮寫:p)
reword:保留該commit剿涮,但我需要修改該commit的注釋(縮寫:r)
edit:保留該commit, 但我要停下來修改該提交(不僅僅修改注釋)(縮寫:e)
squash:將該commit和前一個commit合并(縮寫:s)
fixup:將該commit和前一個commit合并言津,但我不要保留該提交的注釋信息(縮寫:f)
exec:執(zhí)行shell命令(縮寫:x)
drop:我要丟棄該commit(縮寫:d)
根據(jù)我們需要編輯指令后保存,就可以完成commit的合并了取试。
git rebase合并其他分支與git cherry-pick異曲同工悬槽,這里不再復(fù)述了。
上面文章中也說到瞬浓,git rebase會修改commit 記錄初婆,屬于危險操作,需小心操作猿棉。
參考:
rebase 用法小結(jié)