What is Git cherry-pick and how to use it?

1. Brief description

Git is something we use every day. I believe everyone is familiar with branches. This article will introduce you to a very useful git command.

git cherry-pick, we can probably guess by pick, this is a selection, yes, git cherry-pick is simply understood as "selecting" a certain commit, and git cherry-pick commitId will get the commit of this branch The code and related information will be introduced as a new commit to your current branch.

When we need to merge a certain commit of another branch in a branch, and we don’t want to merge the entire branch, but only want to merge a certain commit into the local current branch, then git cherry-pick commitId will be sent It worked.

2. git cherry-pick

2.1 git cherry-pick commitId

Basic usage

git branch
# 当前分支为 test
git log 
# 此时会出现test分支的提交记录,假设此时你想要将
# test分支的commitId为123456的提交合并到sprint分支
git checkout sprint
git cherry-pick 123456
# 此时便已经将这个commit合并到sprint分支了
git log
# 通过git log 便可以看到

Assume the following scenario:

  1. The sprint-9 branch was cut out from the test branch at 9 o'clock,
  2. But at 10 o'clock, I submitted the code that requires sprint-8 on the test branch (a temporary increase in demand)

Therefore, the code of the test branch and the sprint-9 branch are inconsistent at this time.
Of course, if the code sprint-9 modified on the test branch at 10 o'clock is not involved, then naturally there will be no conflicts when merging later, then it does not matter.

However, just in case, no one can guarantee that the above situation will not happen, so here we choose to synchronize the commit of the test branch to sprint-9, and use git cherry-pick commitID to achieve

git cherry-pick commitID

The commitId here is the Id submitted on the test branch at 10 o'clock. At this time, you will find that the commit record will be synchronized to the sprint-9 branch.

2.2 git cherry-pick -n commitId

git cherry-pick will automatically submit. If you don’t want git cherry-pick to submit automatically, just add the parameter -n.
At this point, git cherry-pick -n commitId just merges the code of this commitId into the current branch, but it will not proceed. Submit, you can submit after the modification is completed

2.3 git cherry-pick -e commitId

If you want to re-edit the submission information after git cherry-pick, use the git cherry-pick -e commitId command, then you can re-edit the submission information

Guess you like

Origin blog.csdn.net/qq_41800366/article/details/107067991