GIT Cherry Pick

GIT Cherry Pick

git cherry-pick is a useful tool but not always a best practice.

·

2 min read

Introduction

Git cherry-pick is a command that allows you to select and apply a specific commit from one branch to another branch. This can be useful when you want to apply a single change or a specific set of changes from one branch to another, without merging the entire branch.

Cherry Pick Visually

Cherry Pick git commands

Pick single commit

git cherry-pick <commit hash>

After cherry-picking, the branches' commit history looks like the above image.

Pick multiple commits

git cherry-pick <hash1> <hash2> ... <commit hash n>

Cancel Cherry Pick

git cherry-pick --abort

Example: Trying to run git cherry-pick <commit hash> and encounter merge conflicts. Here git cherry-pick operation is not complete yet. So you can abort the process with a simple git command.

git reset --hard HEAD^

Example: Cherry-pick operation is already completed and needs to go back to the previous state. So use the git reset command.

What is the difference between git cherry-pick and merge

git cherry-pick

Git cherry-pick is used to apply specific commits from one branch to another.

git merge

Git merge is used to combine the work of two or more branches, it brings all commits.

Cherry Pick command with options

  1. Cherry-pick from another branch

     git cherry-pick <commit hash>
    
  2. Edit the commit message of a cherry-picking commit

     git cherry-pick -e <commit hash>
    
  3. After resolving the merge conflict to proceed cherry-pick the command

     git cherry-pick --continue
    
  4. Cancel the current cherry-pick operation

     git cherry-pick --abort
    
  5. Multiple commit picking

     git cherry-pick <first commit hash>..<last commit hash>
    
  6. Cherry-pick commit changes without committing

     git cherry-pick -n <commit hash>
    

Further Readings...

Thank You ❤️