Start
- Yesterday's blog
git
learnedstash
the command in , and today I will learn another command:cherry-pick
.
1. Introduction to cherry-pick
In our normal work scenario, the code in the merged branch is often achieved through merge
the command .
But in some cases, it is cherry-pick
often .
cherry-pick
English translation: select/select
Scenarios I encountered:
情况一:
But I recently encountered such a situation that several branch codes submitted by myself were overwritten by other people's codes. Although the code is covered, the commit submitted by the code still exists. At this time, I can use cherry-pick to merge some of my own commits.
情况二:
I am on the main master
branch , and suddenly want to merge a certain function branch commit
, but the full amount merge
does not meet my requirements, so I can try it at this time cherry-pick
.
2. Specific use
First of all, I have another A branch, and there are several commits on the A branch, as shown in the figure below:
A分支:通过 `git log --oneline` 查看所有commit记录
初次提交->1->2->3
Now I switch to master
the branch , and I want to get commit
the content of the commit record 2 on the A branch.
You can see the screenshot of the appeal, we successfully took the content commit
of .
commit
How to do it if you want to get more than one ?
git cherry-pick commitA commitB commitC
If you want to get a range of commits
git cherry-pick commitA...commitB
# 不包含commitA,包含commitB
git cherry-pick commitA^...commitB
# 包含commitA,包含commitB
Whether it is for a single commit
object cherry-pick
or batch processing, be sure to process it according to the timeline and in the order commit
of the objects, otherwise there will be unexpected problems.
3. cherry-pick summary
To summarize what we learned today:
cherry-pick
The meaning of choice.- We can pick a part of what we need
commit
and merge it into our current branch, wecommit的id
can rely on it.