您的位置:首页 > 其它

Git cherry-pick的使用

2017-03-09 11:07 183 查看
这几天在帮朋友做一个项目,没时间写博客,今天抽时间继续。今天主要讲解一个高级的语法点
cherry-pick。

我们实际开发中肯定遇到过这样的场景,我在develop分支开发了一段时间,我们develop开发中假如提交了三次,我们分别实现了不同的模块功能1,2,3,这时候正赶上老大“心血来潮”,需要把其中模块2提前上线,这时候我们怎么办??难道在建分支,然后回退,合并吗,不是说不能实现,但有没有更好的解决方案呢?其实git早已考虑到这一点。这就是cherry-pick。

herry-pick就是从不同的分支中捡出单独的commit,并把它和你当前的分支合并。但有一点需要注意,如果你pick第二个commit,第一个commit是不会被pick的。具体请看实例:

zxdeMacBook-Pro:hswallpager zs$ git branch
* develop
fromdevelop-02
hotfixes
master
zxdeMacBook-Pro:hswallpager zs$ git checkout -b fromdevelop-1
Switched to a new branch 'fromdevelop-1'
zxdeMacBook-Pro:hswallpager zs$ git add .
zxdeMacBook-Pro:hswallpager zs$ git commit -m "pick-01"
[fromdevelop-1 f41dd9c] pick-01
1 file changed, 2 insertions(+)
zxdeMacBook-Pro:hswallpager zs$ git add .
zxdeMacBook-Pro:hswallpager zs$ git commit -m "pick-2"
[fromdevelop-1 3b45afc] pick-2
1 file changed, 2 insertions(+), 2 deletions(-)
zxdeMacBook-Pro:hswallpager zs$ git log
commit 3b45afc3a1ba8a728e105a7d5c2a047c11517d9c
Author: zhangshun <hbzhangshun@126.com>
Date: Thu Mar 9 13:30:02 2017 +0800

pick-2

commit f41dd9c603bac4b9bb4d0a82855463ec42da0b80
Author: zhangshun <hbzhangshun@126.com>
Date: Thu Mar 9 13:28:39 2017 +0800

pick-01

commit bd36e58e56a81ba77d11a28aeb8537dc7bc21d3c
Author: zhangshun <hbzhangshun@126.com>
Date: Mon Feb 27 22:16:13 2017 +0800

new commit

commit 1976db2eafe93ca1e374cdd87ee708220f42a991
Author: zhangshun <hbzhangshun@126.com>
Date: Mon Feb 27 12:32:22 2017 +0800

develop branch second commit rebase
zxdeMacBook-Pro:hswallpager zs$ git checkout develop
Switched to branch 'develop'
zxdeMacBook-Pro:hswallpager zs$ git cherry-pick pick-02
fatal: bad revision 'pick-02'
zxdeMacBook-Pro:hswallpager zs$ git cherry-pick 3b45af
error: could not apply 3b45afc... pick-2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
zxdeMacBook-Pro:hswallpager zs$ git add .
zxdeMacBook-Pro:hswallpager zs$ git commit -m "合并"
[develop 405746b] 合并
Date: Thu Mar 9 13:30:02 2017 +0800
1 file changed, 2 insertions(+)
zxdeMacBook-Pro:hswallpager zs$ git log
commit 405746b3fbb418b83e83ac1a36532456fc6fc465
Author: zhangshun <hbzhangshun@126.com>
Date: Thu Mar 9 13:30:02 2017 +0800

合并

commit bd36e58e56a81ba77d11a28aeb8537dc7bc21d3c
Author: zhangshun <hbzhangshun@126.com>
Date: Mon Feb 27 22:16:13 2017 +0800

new commit

commit 1976db2eafe93ca1e374cdd87ee708220f42a991
Author: zhangshun <hbzhangshun@126.com>
Date: Mon Feb 27 12:32:22 2017 +0800

develop branch second commit rebase

commit b5dcd26e42c715831ca24810d3080fd25dd92346
Author: zhangshun <hbzhangshun@126.com>
Date: Mon Feb 27 12:31:41 2017 +0800

develop branch first commit rebase

我先是从develop分支新开的分支fromdevelop-01,然后commit两次,这时候我develop分支只需要第二次提交的信息,步骤:
1.git checkout develop

2.git cherry-pick 第二次commitID

3.resolving the conflicts

4.add ,commit

查看本地文件修改可以确认,第一次提交的并没有合并进来,这就是cherry-pick的妙用。

需要说明的是:cherry-pick不但可以用在不同分支之间,也可以用在同一个分支上。简单说一个场景吧:比如今天你新增了一个功能,commit1,第二天这个功能又不需要了,你又不情愿的注释掉,甚至删除了代码,commit2. 然后过了几天,产品过来了,来来来,老弟,还是需要加这个功能,这时候你拿出来菜刀,但产品经理这次却不吃那一套,好吧,那就找回来吧。这时候就可以用cherry-pick,重新找回来了。

另外,也可以cherry-pick多个,最好是最早提交的放在最上边,不同commitID之间,用空格分开。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git cherry-pick