git cherry-pick

git cherry-pick

git cherry-pick 教程
一直迷惑git cherry-pick是把某个提交应用到当前分支,他指的到底是什么意思呢?

  1. 如果这个提交相对于他的上一次提交有新增的文件,有增加的文件
  2. 对于这个提交之前的数据就不会应用到当前分支吗?

新增文件

$ git log --graph --oneline --decorate --all
* 78d5407 (HEAD -> dev) 4.sh
* 1fbbc56 3.sh
| * d60b8a4 (master) 2.sh
| * e64bfcb 1gain.sh
|/
* fda508a again uto
## cherry-pick 
lixi@tom MINGW64 /d/gitl/haha (dev)
$ git cherry-pick d60b8a4
[dev d51b00b] 2.sh
 Date: Thu Dec 24 23:13:28 2020 +0800
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2.sh

lixi@tom MINGW64 /d/gitl/haha (dev)
$ ll
total 0
-rw-r--r-- 1 w20266 1049089 0 十二月 24 23:21 2.sh
-rw-r--r-- 1 w20266 1049089 0 十二月 24 23:15 3.sh
-rw-r--r-- 1 w20266 1049089 0 十二月 24 23:15 4.sh
-rw-r--r-- 1 w20266 1049089 0 十二月 24 23:02 auto.sh

$ git log --graph --oneline --decorate --all
* d51b00b (HEAD -> dev) 2.sh
* 78d5407 4.sh
* 1fbbc56 3.sh
| * d60b8a4 (master) 2.sh
| * e64bfcb 1gain.sh
|/
* fda508a again uto

可以看到只有2.sh来了,1.sh没有过来。所以只有这个commit的变化才会应用到当前分支。

新增文件,增加内容,提交3次

## 新增文件,增加内容,提交3次
$ git log --all --oneline --graph
* 6cb16ad (master) 5.sh 2 line
* 4af2c7b 5.sh first line
* ae7bca7 5.sh
* d60b8a4 2.sh
* e64bfcb 1gain.sh
| * d51b00b (HEAD -> dev) 2.sh
| * 78d5407 4.sh
| * 1fbbc56 3.sh
|/
* fda508a again uto

## cherry-pick出错
lixi@tom MINGW64 /d/gitl/haha (dev)
$ git cherry-pick 6cb16ad
error: could not apply 6cb16ad... 5.sh 2 line
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'

## 让add or rm
lixi@tom MINGW64 /d/gitl/haha (dev|CHERRY-PICKING)
$ git status
On branch dev
You are currently cherry-picking commit 6cb16ad.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)

        deleted by us:   5.sh

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

lixi@tom MINGW64 /d/gitl/haha (dev|CHERRY-PICKING)
$ ll
total 1
-rw-r--r-- 1 w20266 1049089  0 十二月 24 23:21 2.sh
-rw-r--r-- 1 w20266 1049089  0 十二月 24 23:25 3.sh
-rw-r--r-- 1 w20266 1049089  0 十二月 24 23:25 4.sh
-rw-r--r-- 1 w20266 1049089 20 十二月 24 23:26 5.sh
-rw-r--r-- 1 w20266 1049089  0 十二月 24 23:02 auto.sh

## 选择add 
lixi@tom MINGW64 /d/gitl/haha (dev|CHERRY-PICKING)
$ git add 5.sh

lixi@tom MINGW64 /d/gitl/haha (dev|CHERRY-PICKING)
$ git status
On branch dev
You are currently cherry-picking commit 6cb16ad.
  (all conflicts fixed: run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:

        new file:   5.sh


lixi@tom MINGW64 /d/gitl/haha (dev|CHERRY-PICKING)
$ git commit -m 'cherry-pick 5.sh'
[dev 58dfc30] cherry-pick 5.sh
 Date: Thu Dec 24 23:25:02 2020 +0800
 1 file changed, 2 insertions(+)
 create mode 100644 5.sh
## 好了
lixi@tom MINGW64 /d/gitl/haha (dev)
$ git status
On branch dev
nothing to commit, working tree clean

lixi@tom MINGW64 /d/gitl/haha (dev)
$ git log --all --oneline --graph
* 58dfc30 (HEAD -> dev) cherry-pick 5.sh
* d51b00b 2.sh
* 78d5407 4.sh
* 1fbbc56 3.sh
| * 6cb16ad (master) 5.sh 2 line
| * 4af2c7b 5.sh first line
| * ae7bca7 5.sh
| * d60b8a4 2.sh
| * e64bfcb 1gain.sh
|/
* fda508a again uto
## 所有的变化
lixi@tom MINGW64 /d/gitl/haha (dev)
$ cat 5.sh
first line
2 line

总结

所以只有这个commit的变化才会应用到当前分支。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值