Overview explanation:

Git cherry-pick can be used when you need to merge one or more commits from one branch to another current branch

Usage:

git cherry-pick [<options>] <commit-ish>...
Copy the code
  1. A singlecommitSubmit to merge
Git cherry-pick commit commit to the current branchCopy the code

Git cherry-pick = git cherry-pick = git cherry-pick = git cherry-pick

git cherry-pick <branchname>
Copy the code
  1. multiplecommitReferring to merge
git cherry-pick commit1.. Git cherry-pick commit1^.. git cherry-pick commit1^.. Merge all branches between COMMIT1 and COMMIT2 [COMMIT1, commit2]Copy the code
Commonly used options: --quit quit the current cherry pick sequence --continue continue the current cherry pick sequence --abort abort the current cherry pick sequence, Restore the current branch -n, --no-commit Does not commit automatically -e, --edit Edits the commit informationCopy the code

View the last n commits under the current branch and display them on a single line

git log- oneline - nCopy the code

Case 1:

When the automatic commit is not successful, a conflict exists

  1. Resolve the conflict manually in the editor, after the conflict is resolved.
  2. usegit commitManually submit orgit add .Directly afterGit cherry - pick - continueContinue to

Situation 2:

This command is automatically submitted when no code conflicts occur

Using this command will continue cherry-pick after using git add

git cherry-pick --continue
Copy the code

If you cancel a cherry-pick merge, the current branch reverts to the status before cherry- Pick

git cherry-pick --abort
Copy the code

If a cherry-pick merge is interrupted, unconflicted content in the current branch is in the Modified state

git cherry-pick --quit
Copy the code

There is no automatic merge commit at merge time

git cherry-pick -n
Copy the code

Reedit the commit information when merging

git cherry-pick -e 
Copy the code

Q&A

Question 1

Fatal: You are in the middle of a cherry-pick — cannot amend.

The reason:

If a conflict occurs while cherry-pick, execute git commit –amend without resolving the conflict to prompt this message.

Solution:

First resolve the conflict before git commit — Amend and do the cherry pick:

$ git add .
$ git cherry-pick --continue
Copy the code

Question 2

The previous cherry-pick is now empty, possibly due to conflict resolution.

The reason:

If a cherry-pick conflict occurs, the contents of the local branch will not change from those before cherry-pick. Therefore, if you continue git cherry-pick or execute other commands in the next step, since it is still in the last cherry-pick, Is displayed, indicating that the last cherry-pick content was empty due to conflict resolution.

Solution:

  1. performgit cherry-pick --abortCancel the previous operation.
  2. performgit commit --allow-emptyIndicates that empty commit is allowed.