Cherry-pick for git leak detection and filling

Start

  • Yesterday's blog gitlearned stashthe 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 mergethe command .

But in some cases, it is cherry-pickoften .

cherry-pickEnglish 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 masterbranch , and suddenly want to merge a certain function branch commit, but the full amount mergedoes 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:

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-98PNZGk9-1679407247760)(/Users/tomato/Library/Application Support/typora-user-images/image-20230321214248454 .png)]

A分支:通过 `git log --oneline` 查看所有commit记录

初次提交->1->2->3

Now I switch to masterthe branch , and I want to get committhe content of the commit record 2 on the A branch.

insert image description here

You can see the screenshot of the appeal, we successfully took the content commitof .

commitHow 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 commitobject cherry-pickor batch processing, be sure to process it according to the timeline and in the order commitof the objects, otherwise there will be unexpected problems.

3. cherry-pick summary

To summarize what we learned today:

  • cherry-pickThe meaning of choice.
  • We can pick a part of what we need commitand merge it into our current branch, we commit的idcan rely on it.

Guess you like

Origin blog.csdn.net/wswq2505655377/article/details/129698785