KandZ – Tuts

We like to help…!

Git 🧩 4c Cherry Picking

4c Cherry-Picking

Cherry-picking is a powerful Git command that allows you to apply a specific commit from one branch to another. Instead of merging entire branches, cherry-picking lets you selectively choose individual commits, which is particularly useful for applying hotfixes or taking specific changes from feature branches.

Basic Cherry-Pick Command

git cherry-pick [commit-hash]
This command applies the changes introduced by a specific commit to your current branch, creating a new commit with a different commit hash.

git cherry-pick abc1234

Common Options and Flags

-n or –no-commit: Apply changes but don’t commit automatically
-e or –edit: Edit the commit message before committing
-m or –mainline: Specify which parent branch to consider as the mainline
-s or –signoff: Add a Signed-off-by line to the commit

Practical Examples

  • Example 1: Applying a Hotfix from Main Branch
    Let’s say you’re working on a feature branch but need to apply a critical bug fix that was committed to the main branch:

You’re on your feature branch

$ git checkout feature-branch
Switched to branch 'feature-branch'

Check the commit history

$ git log --oneline
abc1234 Fix critical security vulnerability
def5678 Add user authentication
ghi9012 Implement dashboard UI

Apply the security fix to your feature branch

$ git cherry-pick abc1234
[feature-branch 456789a] Fix critical security vulnerability
 Date: Mon Jan 1 12:00:00 2024 -0500
 1 file changed, 5 insertions(+), 2 deletions(-)

Verify the change was applied

$ git log --oneline
456789a Fix critical security vulnerability
def5678 Add user authentication
ghi9012 Implement dashboard UI
  • Example 2: Applying Multiple Commits
    You can cherry-pick multiple commits in sequence:

Apply commits one by one

$ git cherry-pick abc1234 def5678 ghi9012
[feature-branch 456789a] Fix critical security vulnerability
[feature-branch 567890b] Add user authentication
[feature-branch 678901c] Implement dashboard UI

Or apply a range of commits
$ git cherry-pick abc1234..ghi9012

  • Example 3: Cherry-Picking from a Different Branch
    Suppose you have a stable branch with bug fixes that you want to incorporate into your development branch:
    Switch to your development branch
$ git checkout develop
Switched to branch 'develop'

Check what commits are available in the stable branch

$ git log --oneline stable-branch
abc1234 Fix database connection issue
def5678 Optimize query performance
ghi9012 Resolve memory leak

Cherry-pick these commits into develop

$ git cherry-pick abc1234 def5678 ghi9012
[develop 789012a] Fix database connection issue
[develop 890123b] Optimize query performance
[develop 901234c] Resolve memory leak

Verify the commits were applied

$ git log --oneline
901234c Resolve memory leak
890123b Optimize query performance
789012a Fix database connection issue
def5678 Add user authentication
ghi9012 Implement dashboard UI

Advanced Cherry-Pick Scenarios

  • Cherry-Picking a Range of Commits
    Apply commits from abc1234 up to ghi9012 (inclusive)
    $ git cherry-pick abc1234..ghi9012

Apply commits from abc1234 up to ghi9012, excluding abc1234

$ git cherry-pick abc1234^..ghi9012

Apply a single commit with a specific message
$ git cherry-pick -m "This is a custom commit message" abc1234

  • Cherry-Picking with Conflicts
    When cherry-picking commits that conflict with your current working directory:
$ git cherry-pick abc1234
Auto-merging src/app.js
CONFLICT (content): Merge conflict in src/app.js
error: could not apply abc1234
hint: After resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

Resolve conflicts manually then stage the resolved files

$ git add src/app.js

Complete the cherry-pick
$ git commit

  • Cherry-Picking Without Creating a New Commit
    Apply changes without creating a new commit (squash mode)
    $ git cherry-pick -n abc1234

Can you answer these Questions

  1. What is the primary purpose of git cherry-pick and when would you use it instead of merging?
  2. How does cherry-picking create a new commit compared to other Git operations like merge or rebase?
  3. Can you cherry-pick a commit that has already been cherry-picked into your current branch? What happens in such a case?
  4. What is the difference between git cherry-pick abc1234 and git cherry-pick abc1234..ghi9012?
  5. How do you resolve conflicts that occur when using git cherry-pick?
  6. What are the advantages and disadvantages of using cherry-picking in a collaborative environment?
  7. When might you use git cherry-pick -n and what is its practical application?
  8. What happens to the original commit when you cherry-pick it into another branch? Does it get removed from the source branch?
  9. How can you see which commits have already been cherry-picked into your current branch?
  10. What is the purpose of using git cherry-pick -m with a custom message?
  11. Why might you choose to cherry-pick specific commits rather than merging entire branches in a feature workflow?
  12. How does git cherry-pick handle commits that were made on different branches but affect the same files?
  13. What is the significance of the commit hash being different after a cherry-pick operation?
  14. Can you cherry-pick commits from a remote branch, and if so, how would you do it?
  15. What are some best practices for managing cherry-picked commits in your project history?
  16. When using git cherry-pick with multiple commits, what happens if one commit fails due to conflicts?
  17. How does the –signoff flag work with git cherry-pick, and when might you use it?
  18. What are some common mistakes people make when using git cherry-pick?
  19. How can you undo a cherry-pick if you realize it was applied incorrectly?
  20. In what scenarios would you prefer using git cherry-pick over creating a new branch and merging specific changes?

Leave a Reply