git cherry-pick简介

1. 苦逼的我

我实际工作中的使用场景如下:
   一开始我不知道这个命令,所以当修复线上某个bug或者线上要临时增加一个新功能时,我都是在dev分支上开发完,然后把刚刚提交到dev分支上的这一小点代码小心翼翼的再复制一份到prod分支.今天同事突然给我说这样容易出错,应该使用git cherry-pick命令::>_<::

2.一个例子讲讲git cherry-pick

  • dev分支提交历史如下(其实这里就可以看出master的提交历史)
27242@LAPTOP-V7UUU6JC MINGW64 ~/Desktop/A/GitTest (dev)
$ git log --oneline --graph -8  //查看最近8条提交历史
* a839ba4 (HEAD -> dev) dev004
* 91aa40a AAA:我是dev000003
* 5239520 A:我是dev0002
* 5a43bb3 AAA:我是dev001
* 492cb16 (origin/master, origin/dev, origin/HEAD, master) AAA:master03
* f661338 AAAA:masterAAAA02
* 81da371 AAA:masterAAA01
* fa28c53 git cherry-pick学习
  • master分支提交历史如下
27242@LAPTOP-V7UUU6JC MINGW64 ~/Desktop/A/GitTest (master)
$ git log --oneline -4 --graph #这样也可以
* 492cb16 (HEAD -> master, origin/master, origin/dev, origin/HEAD) AAA:master03
* f661338 AAAA:masterAAAA02
* 81da371 AAA:masterAAA01
* fa28c53 git cherry-pick学习

假如我想只把dev分支上commit id为a839ba4 的commit弄到master分支,具体操作如下:

27242@LAPTOP-V7UUU6JC MINGW64 ~/Desktop/A/GitTest (master)
$ git cherry-pick a839ba4
error: could not apply a839ba4... dev004
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add ' or 'git rm '
hint: and commit the result with 'git commit'

发现有冲突了,需要用git status看一下哪个文件产生了冲突:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

You are currently cherry-picking commit a839ba4.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add ..." to mark resolution)

        both modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

发现冲突文件为 README.md
解决冲突,并将冲突文件进行git add添加到暂存区,然后接着用git cherry-pick --continue命令继续进行cherry-pick,操作如下:

27242@LAPTOP-V7UUU6JC MINGW64 ~/Desktop/A/GitTest (master|CHERRY-PICKING)
$ git add README.md

27242@LAPTOP-V7UUU6JC MINGW64 ~/Desktop/A/GitTest (master|CHERRY-PICKING)
$ git cherry-pick --continue
[master 7e7e283] dev004
 Date: Wed Feb 20 22:54:31 2019 +0800
 1 file changed, 7 insertions(+)

27242@LAPTOP-V7UUU6JC MINGW64 ~/Desktop/A/GitTest (master)
$ git log --oneline  -4 #这样也可以
* 7e7e283 (HEAD -> master) dev004
* 492cb16 (origin/master, origin/dev, origin/HEAD) AAA:master03
* f661338 AAAA:masterAAAA02
* 81da371 AAA:masterAAA01

注意git cherry-pick --continue后会进入如下页面:
git cherry-pick简介_第1张图片
也就是这里其实进行了一次新的提交.

3.其他

当然git cherry-pick还有一些其他的操作,这里就不再细讲了,推荐一篇博客: 博客地址

你可能感兴趣的:(git,cherry-pick,git)