For a multi-branch code base, moving code from one branch to another is a common requirement.

There are two things going on here. In one case, you need all the code changes in another branch, so use git merge. On the other hand, when you only need some code changes (a few commits), Cherry Pick can be used.

First, basic usage

The git cherry-pick command applies the specified commit to another branch.

$ git cherry-pick <commitHash>
Copy the code

The above command applies the specified commit, commitHash, to the current branch. This will result in a new commit in the current branch, but of course their hashes will be different.

For example, a code repository has two branches: Master and feature.

    a - b - c - d   Master
         \
           e - f - g Feature
Copy the code

Now apply commit F to the master branch.

Switch to the master branch
$ git checkout master

# Cherry pick operation
$ git cherry-pick f
Copy the code

When the above is done, the code base should look like this.

    a - b - c - d - f   Master
         \
           e - f - g Feature
Copy the code

As you can see above, a commit F is added to the end of the master branch.

Git cherry-pick is a parameter to the git cherry-pick command, which is not necessarily the commit hash, but the branch name, which represents the latest commit of the branch to be transferred.

$ git cherry-pick feature
Copy the code

The above code indicates that the latest submission of the feature branch is transferred to the current branch.

2. Move multiple commits

Cherry Pick supports moving multiple commits at once.

$ git cherry-pick <HashA> <HashB>
Copy the code

The command above applies both commits A and B to the current branch. This generates two new commits corresponding to the current branch.

If you want to move a series of consecutive commits, you can use the following handy syntax.

$ git cherry-pick A.. BCopy the code

The command above can divert all commits from A to B. They must be placed in the correct order: Commit A before commit B, or the command will fail without an error.

Note that with the command above, commit A will not be included in Cherry Pick. If you want to include commit A, you can use the following syntax.

$ git cherry-pick A^.. BCopy the code

3. Configuration items

The common configuration items of the git cherry-pick command are as follows:

(1)-e.--edit

Open the external editor to edit the submission information.

(2)-n.--no-commit

Only the workspace and staging areas are updated, and no new commits are generated.

(3)-x

Add a line at the end of the commit information. , so that you can later find out how the commit was generated.

(4)-s.--signoff

Append a line of the operator’s signature to the end of the submission to indicate who did the action.

(5)-m parent-number.--mainline parent-number

If the original commit is a merge node from a merge of two branches, Cherry Pick will fail by default because it does not know which branch’s code changes should be adopted.

The -m configuration item tells Git which branch changes should be adopted. Its argument parent-number is an integer starting from 1, representing the parent branch number of the original submission.

$ git cherry-pick -m 1 <commitHash>
Copy the code

The above command indicates that Cherry pick takes a change to submit commitHash from the parent branch numbered 1.

Generally speaking, parent 1 is the branch being merged into, and parent 2 is the branch being merged from.

4. Code conflicts

If a code conflict occurs during the operation, Cherry Pick stops and lets the user decide how to proceed.

(1)--continue

Once the user resolves the code conflicts, the first step is to add the modified files back to the staging area (git add.) The second step is to let the Cherry Pick process continue with the following command.

$ git cherry-pick --continue
Copy the code

(2)--abort

After a code conflict, abandon the merge and return to the way it was before the operation.

(3)--quit

Exit Cherry Pick after a code conflict, but do not return to the way it was before the operation.

Move to another code base

Cherry Pick also supports moving a commit from another code base by first adding it as a remote repository.

$ git remote add target git://gitUrl
Copy the code

The above command adds a remote repository target.

Then, grab the remote code locally.

$ git fetch target
Copy the code

The command above captures the remote code repository locally.

Next, examine the commit to be moved from the remote repository to get its hash value.

$ git log target/master
Copy the code

Finally, use the git cherry-pick command to divert the commit.

$ git cherry-pick <commitHash>
Copy the code

(after)

The original address: www.ruanyifeng.com/blog/2020/0…