假设:
假设我们有 branch_01 和 branch_02 两个开发分支,那么我们怎么把 branch_01 上的一个或者几个commit合并到 branch_02 上呢?
假设 branch_01 有如下两个commit的hash值
59e7e6545a2eda9b82f5795173792e6490c9cd13
21b385c03a032d90102f1c8321f7858ff8788714
如何查看commit得hash值呢?
使用 $ git log 命令查看。
$ git log
在 commit 右边的就是commit的hash值:
commit 8261c51c2f7aa4282f58e6579dbf35b112d4626b (HEAD -> rel/7.16)
Author: wang_zg <GhostOnen@gmail.com>
Date: Tue Sep 11 17:04:17 2018 +0800
每次commit都会生成一个hash值,这个值是唯一的。
结果:
cherry-pick 在 Git 文档中的解释如下:
Apply the changes introduced by some existing commits
意思是这个命令可以对已经存在的 commit 进行再次提交。
接下来,我们把 branch_01 的两个commit合并到 branch_02 中。
首先我们要切换到 branch_02 分支,合并到哪个分支就切换到哪个分支。
$ git checkout branch_02
$ git cherry-pick -n 59e7e6545a2eda9b82f5795173792e6490c9cd13 21b385c03a032d90102f1c8321f7858ff8788714
注意:
- branch_01 的两个 commit 就被 合并到本地的 branch_02 分支上了,这时候的更改并没有被提交到远程仓库,使用 git status 可以查看所有的更改。
- 多个commit的hash使用空格分割, commit的hash最好按提交时间先后排列, 即最先提交的commit放在前面。
这两行命令执行之后,如果顺利没有报错,就可以使用 git commit , git push 命令进行正常提交到远程仓库,就行。
如果报错了,也就是说在 cherry-pick 过程中产生了冲突,会报如下错误:
Automatic cherry-pick failed. After resolving the conflicts,
mark the corrected paths with 'git add <paths>' or 'git rm <paths>'
那我们只需跟解决普通冲突一样,手动解决就行。
$ git status #查看哪些文件出现了冲突
both modified: app/home/viewcontrollor.m
$ vim app/home/viewcontrollor.m #手动解决
$ git add app/home/viewcontrollor.m
$ git commit #提交
$ git push #推送到远程仓库