Skip to content

Latest commit

 

History

History
176 lines (126 loc) · 4.18 KB

2. Cherry-Picking.md

File metadata and controls

176 lines (126 loc) · 4.18 KB

Cherry-Picking

Cherry-picking in Git allows you to apply specific commits from one branch to another without merging the entire branch. This is useful for selectively integrating changes.


1. What is Cherry-Picking?

Cherry-picking is the process of taking a specific commit from one branch and applying it to another. It lets you:

  • Pick individual commits without merging the entire branch.
  • Fix bugs or apply changes from one branch to another selectively.

2. When to Use Cherry-Picking

  • Bug Fixes: Apply a critical fix from a feature branch to main.
  • Selective Features: Bring specific features or updates from another branch.
  • Undo Mistakes: Reapply changes from a reverted commit.

3. How Cherry-Picking Works

Syntax

git cherry-pick <commit-hash>

Example

Consider the following branches:

feature branch:

A --- B --- C (HEAD)

main branch:

X --- Y --- Z (HEAD)

If you cherry-pick commit B from feature to main, the result is:

X --- Y --- Z --- B'

4. Performing a Cherry-Pick

Step 1: Identify the Commit

List the commits in the source branch:

git log --oneline

Copy the hash of the commit you want to cherry-pick.

Step 2: Cherry-Pick the Commit

Switch to the target branch and apply the commit:

git checkout main
git cherry-pick <commit-hash>

Step 3: Resolve Conflicts (if any)

If conflicts arise:

  1. Open and resolve the conflicting files.
  2. Stage the resolved files:
    git add resolved_file.txt
  3. Complete the cherry-pick:
    git cherry-pick --continue

To abort the cherry-pick:

git cherry-pick --abort

5. Cherry-Picking Multiple Commits

To cherry-pick multiple commits:

  1. Use individual hashes:
    git cherry-pick <hash1> <hash2> <hash3>
  2. Cherry-pick a range of commits:
    git cherry-pick <hash1>^..<hash3>

6. Undoing a Cherry-Pick

If you need to undo a cherry-picked commit:

git revert <commit-hash>

7. Interactive Cherry-Picking

You can use interactive rebasing to cherry-pick commits:

  1. Start an interactive rebase:
    git rebase -i <base-branch>
  2. Select and reorder the commits you want to apply.

8. Best Practices for Cherry-Picking

  • Minimize Conflicts: Keep the source and target branches up-to-date to reduce conflicts.
  • Document the Reason: Use meaningful commit messages to explain why the commit was cherry-picked.
  • Verify Changes: Test the branch after cherry-picking to ensure functionality.

9. Common Commands

Command Description
git cherry-pick <hash> Apply a specific commit to the current branch.
git cherry-pick <hash1> <hash2> Apply multiple commits.
git cherry-pick <hash1>^..<hash2> Apply a range of commits.
git cherry-pick --continue Continue after resolving conflicts.
git cherry-pick --abort Cancel a cherry-pick.

10. Example Workflow

Step 1: Identify the Commit

Find the commit hash from the source branch:

git log feature --oneline

Step 2: Switch to the Target Branch

git checkout main

Step 3: Cherry-Pick the Commit

git cherry-pick <commit-hash>

Step 4: Test and Verify

Run tests to ensure the changes work as intended.


11. When to Avoid Cherry-Picking

  • Frequent Use: Excessive cherry-picking can lead to a fragmented history.
  • Public Commits: Avoid cherry-picking public commits that might cause confusion for collaborators.

Conclusion

Cherry-picking is a powerful tool for applying specific commits across branches without merging. When used appropriately, it ensures targeted changes while maintaining control over your repository.


Next Steps: Stashing Changes