如果把commit看成一个个的樱桃,我们总是会挑选最大最红的那个吧。cherry-pick就是干这个的,挑选出我们想要的那个commit。我们看看cherry-pick这个命令是怎么用的吧。
挑选一个commit
现在新建两个分支,分别为development和production。假定我们的产品线需要开发线的一个功能,测试并发布。我们应该怎么做呢。
-
先看看我们的开发线:
开发线
-
产品线
产品线
- 现在需要把ec35132这个commit加到产品线上
git cherry-pick ec35132
提示git cherry-pick --edit ec35132可以修改commit的信息
- 查看log
git log --oneline
新的commit
我们可以看到产品线上增加了一个新的commit,是不是很好用啊。看一看图吧。
cherry-pick
选出但不提交
上面我们知道了--edit可以用来修改信息,有没有别的参数呢,这里介绍一个比较实用的参数,可以让我们选择commit,但是不提交,这样当我们往production里面加功能的时候,就可以先检查检查,再手动提交了。来看一下怎么用的。
- 先reset以前的操作(如果做前面的例子可以省略这一步)
git reset --hard HEAD^
- 选择多个并不提交
git cherry-pick --no-commit ec35132 792661d
-
修改再提交
commit
- 产看log
git log --oneline
log
其他有用的命令
git cherry-pick -x ec35132 追加SHA到commit信息
git cherry-pick --signoff ec35132 追加作者信息
| 命令 | 作用 |
|---|---|
| x | When recording the commit, append a line that says "(cherry picked from commit ...)" to the original commit message in order to indicate which commit this change was cherry-picked from. This is done only for cherry picks without conflicts. Do not use this option if you are cherry-picking from your private branch because the information is useless to the recipient. If on the other hand you are cherry-picking between two publicly visible branches (e.g. backporting a fix to a maintenance branch for an older release from a development branch), adding this information can be useful. |
| s | Add Signed-off-by line at the end of the commit message. |
注意:-x命令只在公共分支上才有意义,如果一个分支只有你在用,加这个没啥意思。













网友评论