Learning git (eight): the use of git cherry-pick

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-pickthis 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

masterbranch for releases

devfunction 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 devcompleted 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)

insert image description here

Three functions have been developed here, and now we only need to put A Bthe function online.

So I need to master分支use cherry-pickcommand on .

First of all: We need to record and first a, bonly commitIdthe 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

insert image description here

In this way, A Bthe functions have been merged, but what needs to be noted here is: what commitIDhas 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, commit1it will not be merged into the master branch, but commit100it will be.


use:

insert image description here

switch to the master branch

git checkout master
git cherry-pick 0785f5c670e70609..daba06ba67367
// master c1 不会被合并
// b 会被合并

The effect is the same as


options

cherry-pickEvery 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 -noptions.

git cherry-pick -n 0785f5c670e70609..daba06ba67367

In this way, everything is saved in the cache area, and then you can manually commitwrite 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 

Guess you like

Origin blog.csdn.net/James_xyf/article/details/120817391