Cross-Branch Commits
Understanding Cross-Branch Commits
Cross-branch commits allow developers to apply changes from one branch to another, providing flexibility in code management and collaboration.
Cherry-Pick: Selective Commit Transfer
Basic Cherry-Pick Operation
## Switch to target branch
git checkout target-branch
## Cherry-pick specific commit
git cherry-pick <commit-hash>
Cherry-Pick Workflow Visualization
gitGraph
commit
branch feature-branch
checkout feature-branch
commit
commit
checkout main
cherry-pick 2nd-commit
Methods of Cross-Branch Commits
Method |
Description |
Use Case |
Cherry-Pick |
Transfer specific commits |
Selective code migration |
Merge |
Combine entire branch history |
Integrating feature branches |
Rebase |
Replay commits on another branch |
Maintaining linear history |
Advanced Cherry-Pick Techniques
## Cherry-pick with preserving original commit message
git cherry-pick -x <commit-hash>
## Cherry-pick multiple commits
git cherry-pick <commit1-hash> <commit2-hash>
## Cherry-pick commit range
git cherry-pick <start-commit>..<end-commit>
Handling Conflicts
When cherry-picking commits, conflicts may arise:
## Resolve conflicts manually
git cherry-pick <commit-hash>
## Edit conflicting files
git add .
git cherry-pick --continue
LabEx Recommendation
Practice cross-branch commit techniques in LabEx's interactive Git environments to build practical skills.
Common Scenarios
- Backporting bug fixes
- Applying hotfixes across branches
- Selectively moving development work
Best Practices
- Always check branch status before cherry-picking
- Communicate with team about cross-branch commits
- Verify commit compatibility
- Use meaningful commit messages