Simple tutorial on git cherry-pick

  sonic0002        2020-05-02 00:02:58       8,616        0    

It is a common operation to move code from one branch to another branch in a multi-branch git repository. Normally there are two scenarios:

  1. Want to move all changes in one branch to another branch, now it's called git merge 
  2. Want to move only some change/changes from one branch to another branch, now it's called git cherry-pick

Usage

The purpose of git cherry-pick is to apply some change from one branch to another branch. 

git cherry-pick [commitHash]

The above command is to apply the commit commitHash to the current working branch. It will create a new commit in the current working branch with a different unique commit hash. 

For example, if there is a master branch and a feature branch with bellow structure.

a - b - c - d   Master
         \
           e - f - g Feature

If want to apply the code change in commit f to master branch, the steps would be

# switch to master branch
git checkout master

# cherry pick
git cherry-pick f

When above is completed, the repository structure looks like

 a - b - c - d - f'   Master
         \
           e - f - g Feature

A new commit f' has been created at the end of master branch. 

The cherry-pick command can take other parameters like branch name as well apart from commitHash. If a branch name is given, it will take the last commit from the given branch and applies to the current working branch.

git cherry-pick feature

In above example, it means that commit g would be applied to master branch.

 a - b - c - d - g'   Master
         \
           e - f - g Feature

Also git cherry-pick supports apply multiple commits to another branch at once. The syntax is

git cherry-pick [HashA] [HashB]

This command will take two commits A and B and apply them to current working branch and will create two new commits in the current working branch.

If want to apply some consecutive commits to another branch, below command can be used.

git cherry-pick A..B 

One thing to note here is that A must be a commit earlier than B, otherwise the command will fail. In addition, this doesn't include the commit A in the cherry picked commits. To include A as well, need to run the command below.

git cherry-pick A^..B 

Options

Some common options used along with git cherry-pick are

-e,--edit

Open editor to edit commit message

-n,--no-commit

Update only working directory and stash arear but not commit

-x

Append a line (cherry picked from commit ...) at the end of the commit message so that can identify it is a cherry picked commit in the future.

-s,--signoff

Append name of the author at the end of the commit message to indicate who does the operation

-m parent-number,--mainline parent-number

If the commit to be cherry picked is a merge point, it will fail to work by default as it doesn't know which branch to pick the change to cherry pick. This option can indicate to pick the change from which parent branch, the number starts from 1. 

git cherry-pick -m 1 [commitHash]

Normally 1 is the branch being merged into, and 2  is the branch being merged from.

Merge conflicts

If there is merge conflict when doing cherry pick, it will stop and asks the suggestion from user.

--continue

Sometimes when conflicts happen, one may want to resolve the conflicts manually and add the changes to the stash area(git add .) and continue the cherry pick with below command.

git cherry-pick --continue

--abort

Sometimes if there are two many conflicts and it's becoming complicated to manually resolve them, one may want to abort the cherry pick. This will bring the state of the branch to its original state before cherry pick.

git cherry-pick --abort

--quit

Similar to --abort but it will keep whatever changes have been applied before conflict happens This may not be what users want to have most of the time. Don't recommend to use.

git cherry-pick --quit

Cherry pick to other repo

Cherry pick also supports to apply change from one repo to another repo. 

The first step is to add another repo into local environment.

git remote add target git://gitUrl

Then fetch the remote repo to local

git fetch target

Next can check the hash of the commit from remote repo to be cherry picked.

git log target/master

Apply the change with the hash to current working branch

git cherry-pick [commitHash]

Reference: http://www.ruanyifeng.com/blog/2020/04/git-cherry-pick.html

TUTORIAL  GIT  GIT CHERRY-PICK 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Stackoverflow helps over 1 million people to exit Vim