For multi-branch codebases, it is a common requirement to move code from one branch to another.
There are two situations at this time. In one case, you need all the code changes from another branch, then use merge( git merge
). Another situation is that you only need some code changes (certain commits), then Cherry pick can be used.
basic usage
git cherry-pick
The role of the command is to apply the specified commit (commit) to other branches.
$ git cherry-pick <commitHash>
The above command will commitHash
apply the specified commit to the current branch. This will result in a new commit on the current branch, although of course their hashes will be different.
For example, the code repository has master
and feature
two branches.
a - b - c - d Master
\
e - f - g Feature
Now apply f
the commit to master
the branch.
# 切换到 master 分支
$ git checkout master
# Cherry pick 操作
$ git cherry-pick f
After the above operations are completed, the code base will look like the following.
a - b - c - d - f Master
\
e - f - g Feature
As you can see above, master
a commit has been added to the end of the branch f
.
git cherry-pick
The parameter of the command is not necessarily the hash value of the commit, but the branch name is also possible, which means transferring the latest commit of the branch.
$ git cherry-pick feature
The above code means to feature
transfer the latest commit of the branch to the current branch.
transfer multiple commits
Cherry pick supports transferring multiple commits at once.
$ git cherry-pick <HashA> <HashB>
The above command applies two commits A and B to the current branch. This generates two corresponding new commits in the current branch.
If you want to shift a series of consecutive commits, you can use the following convenience syntax.
$ git cherry-pick A..B
The above command can transfer all commits from A to B. They must be placed in the correct order: commit A must be earlier than commit B , otherwise the command will fail without error.
Note that with the above command, commit A will not be included in the cherry pick . If you want to include commit A , you can use the syntax below.
$ git cherry-pick A^..B
The above is git cherry-pick
the basic usage. If you want to know more usages, such as transferring to another code base, code conflicts, configuration items, etc., please visit the link of the original text of this big guy. This article is just porting.
git cherry-pick tutorial - Ruan Yifeng's weblog http://www.ruanyifeng.com/blog/2020/04/git-cherry-pick.html