← Back to Chapters

Git Cherrypick & Patch

? Git Cherrypick & Patch

? Quick Overview

Git Cherrypick and Git Patch are powerful tools used to selectively apply changes from one branch or commit to another without merging entire branches.

? Key Concepts

  • Selective commit application
  • Backporting fixes
  • Sharing changes without history

? What is Git Cherrypick?

git cherrypick allows you to pick a specific commit from one branch and apply it to another branch without merging all commits.

⚙️ How to Use Git Cherrypick

? View Code Example
// Apply a specific commit to the current branch
git cherrypick <commit-hash>

⚠️ Handling Conflicts

? View Code Example
// Stage resolved files after conflict
git add file-name
? View Code Example
// Continue cherrypick after resolving conflicts
git cherrypick --continue
? View Code Example
// Abort cherrypick and rollback changes
git cherrypick --abort

? What is Git Patch?

A Git patch is a file containing differences between commits, useful for sharing or applying changes independently.

?️ Create a Git Patch

? View Code Example
// Create a patch file between two commits
git diff commit1 commit2 > my-patch.patch

? Apply a Git Patch

? View Code Example
// Apply patch without committing
git apply my-patch.patch
? View Code Example
// Apply patch and create commit
git am my-patch.patch

? Cherrypick vs Patch

  • Cherrypick: Preserves commit metadata
  • Patch: Applies changes without history

? Tips & Best Practices

  • Always verify commit hashes
  • Use patches when sharing changes externally
  • Keep history clean after cherrypicking

? Try It Yourself

  • Cherrypick a bug-fix commit
  • Create and apply a patch
  • Resolve cherrypick conflicts manually