Ask yourself a few more whys every day, and you will always get unexpected results. The growth path of a rookie Xiaobai (copyer)
In a recent project, a colleague used git cherry-pick
this command. I was really confused. I had never seen it before, so I came down to learn about it. Learning is always good.
scenes to be used
master
branch for releases
dev
function modules for development
In the actual development process, such a situation may be encountered, so programmers have already written code crazily, and it has been dev
completed on the branch A功能
, B功能
, C 功能
. One day, in order to make money, the boss asked the programmers to put A功能
and B 功能
online first, C功能
and other functions under development to go online next time. So for programmers, it must not be used git merge
, because it will upload the functions currently under development and affect the operation of the program. So, you need to use this command now cherry-pick
.
basic use
git cherry-pick commitId
example
// 初始化
git init
// master的代码提交
touch master.js
git add . && git commit -m 'master c1'
// 创建dev分支
git checkout -b 'dev'
touch a.js
git add . && git commit -m 'a'
touch b.js
git add . && git commit -m 'b'
touch c.js
git add . && git commit -m 'c'
git log(dev)
Three functions have been developed here, and now we only need to put A
B
the function online.
So I need to master分支
use cherry-pick
command on .
First of all: We need to record and first a
, b
only commitId
the first few digits are needed.
a 57c1b614a
b aa1f03dd0999
Next, switch to the master branch
git checkout master
git cherry-pick 57c1b614a
git cherry-pick aa1f03dd0999
then look at
git log
In this way, A
B
the functions have been merged, but what needs to be noted here is: what commitID
has changed here.
but? There is still a problem, if you need to transplant a lot, do you have to execute the command again and again? The answer is definitely not,
intervals are also supported here
git cherry-pick commit1..commit100
But be careful, this is an 左开右闭
operation, that is to say, commit1
it will not be merged into the master branch, but commit100
it will be.
use:
switch to the master branch
git checkout master
git cherry-pick 0785f5c670e70609..daba06ba67367
// master c1 不会被合并
// b 会被合并
The effect is the same as
options
cherry-pick
Every time a commit is selected by the command, a new commit id will be generated once.
What should we do if we want to suspend the submission of each commit after selection, and wait until all the commits are selected and commit manually? The answer is with -n
options.
git cherry-pick -n 0785f5c670e70609..daba06ba67367
In this way, everything is saved in the cache area, and then you can manually commit
write one by yourself.总commit记录
Summarize
Cherry-pick is new knowledge, too little to understand, need to learn
// 命令总结
git cherry-pick commitId
git cherry-pick commitId..commitId (左开右闭)
git cherry-pick -n