Q: You have specific code changes in one Git branch and want to move only those changes to another branch. How would you do this?
Production guide for selectively porting isolated commits and hotfixes between Git branches using git cherry-pick without merging unrelated code.
git cherry-pick is the standard tool. It extracts the diff introduced by specific commits and applies them as new commits onto the target branch."
Want to master this scenario in a live sandbox? KodeKloud's Advanced Git Workflows, Rebase & Interactive Diagnostics covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: When you need to backport a specific bug fix, configuration change, or feature from a development or experimental branch into a release or production hotfix branch without merging the entire history, git cherry-pick is the standard tool. It extracts the diff introduced by specific commits and applies them as new commits onto the target branch.
Identify Commit Hash on Source Branch
Check the git log on the source branch to identify the exact commit SHAs you want to copy.
git checkout feature-branch
git log --oneline -n 5
# Output: a8f3b9c fix(auth): resolve token expiration race condition
Switch to Target Branch and Execute Cherry-Pick
Switch to the target branch (e.g. main or release-v1.4), ensure your working tree is clean, and run git cherry-pick .
git checkout main
git pull origin main
git cherry-pick a8f3b9c
Resolving Merge Conflicts (If Applicable)
If the destination branch has divergent code, Git will pause with conflict markers. Resolve conflicts in your editor, stage the files, and continue the cherry-pick.
# Check conflicting files
git status
# After resolving markers:
git add auth/token.go
git cherry-pick --continue
# (Or run 'git cherry-pick --abort' to cancel cleanly)
Cherry-Picking Multiple Commits or Ranges
To cherry-pick a contiguous range of commits from commit A to commit B:
git cherry-pick A^..B (includes commit A up to commit B).
- Identify the commit SHA from git log on the source branch.
- Checkout the target branch and run git cherry-pick <commit-sha>.
- If conflicts occur, resolve conflict markers, stage changes, and run git cherry-pick --continue.
- Use git cherry-pick A^..B to port a contiguous range of commits cleanly.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Running raw git push --force rather than git push --force-with-lease on shared team feature branches when configuring Moving Specific Code
git push --forceunconditionally overwrites remote commits made by teammates, whereas--force-with-leaseaborts if the remote ref was updated. - Resolving complex merge conflicts with blind checkout overwrite strategies without running unit tests when configuring Moving Specific Code Unilateral conflict resolution frequently clobbers complementary logic introduced in parallel feature branches, creating regression bugs.
-
Discarding corrupted working directories prematurely instead of recovering lost state using git reflog when configuring Moving Specific Code
git reflogtracks every HEAD movement, allowing instant recovery of discarded commits, botched rebases, or deleted local branches. - Disabling branch protection rules and mandatory PR approval reviews on shared release branches when configuring Moving Specific Code Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.