We sometimes encounter in development that we need to synchronize partial modifications of another branch to the current branch.
As shown in the figure below, I want to synchronize commit E and F in the devA branch to the green devB branch below.
At this time, you can use git cherry-pick
to complete this task.
(cherry-pick has the meaning of screening and selection)
1. Basic usage
transfer a single commit
git cherry-pick <commitHash>
# 切换到 devB 分支
$ git checkout devB
# Cherry pick 操作
$ git cherry-pick <HashE>
After the conflict is resolved, commit can
2. Transfer multiple commits
If I have a bunch of continuous commits that I want to synchronize, I can use the following syntax:
The following command can transfer all commits from E to F. Note that writing in order: commit E must be earlier than commit F
git cherry-pick <HashE>..<HashF>
Also note that the above command is left-closed and right-open, that is, commit_E is not included. If you need to include both sides, use the following syntax:
git cherry-pick <HashE>^..<HashF>
If it is several separate commits, you can write like this:
git cherry-pick <HashE> <HashG>
3. Parameters
This is what the documentation says:
usage: git cherry-pick [<options>] <commit-ish>...
or: git cherry-pick <subcommand>
--quit end revert or cherry-pick sequence
--continue resume revert or cherry-pick sequence
--abort cancel revert or cherry-pick sequence
--skip skip current commit and continue
--cleanup <mode> how to strip spaces and #comments from message
-n, --no-commit don't automatically commit
-e, --edit edit the commit message
-s, --signoff add a Signed-off-by trailer
-m, --mainline <parent-number>
select mainline parent
--rerere-autoupdate update the index with reused conflict resolution if possible
--strategy <strategy>
merge strategy
-X, --strategy-option <option>
option for merge strategy
-S, --gpg-sign[=<key-id>]
GPG sign commit
-x append commit name
--ff allow fast-forward
--allow-empty preserve initially empty commits
--allow-empty-message
allow commits with empty messages
--keep-redundant-commits
keep redundant, empty commits
Mention a few things that will come in handy:
1) -n
If you want to transfer multiple commits and only want one commit in the new branch, you can add -n
parameters , not automatically submit the code, and submit them manually at one time after the transfer. (Note that it is not very useful if there is a conflict) (In order to distinguish whether it is transferred from other branches, you can open a new branch to synchronize these commits, and then merge to the target branch)
-n, --no-commit don't automatically commit
2) -x
Add a line (cherry picked from commit ...) at the end of the submission information, so that you can find out how the submission was generated later.
-x append commit name
3) It is not recommended to synchronize the "merge node", the result should not be what you want (if you are interested, you can try it yourself).
4. Code Conflicts
1) --continue
The synchronization code will inevitably encounter conflicts. After resolving the conflicts, re-add the modified files to the temporary storage area git add .
, and then use the following command to continue:
git cherry-pick --continue
2) --abort
There may be misoperations during the processing, so you can abandon the merge and return to the state before the operation.
git cherry-pick --abort
(3) --quit
After a code conflict occurs, exit cherry pick, but do not return to the state before the operation.
git cherry-pick --quit