Merging a Commit from Another Git Branch
You have two branches with diverging histories and need to apply only certain commits from one branch to another. Git’s cherry-pick command lets you do exactly that.
The scenario
Say you have:
dev branch:
c0 → c1 → c2 → c3 → c4
master branch:
c0 → c5 → c6
Commits c2 and c3 fix a critical bug. You want those fixes on master without pulling in c4 (which might be incomplete work or experimental changes).
Using git cherry-pick
Cherry-pick takes one or more commits and applies them to your current branch. First, check out the target branch:
git checkout master
Then cherry-pick the specific commits using their commit hashes:
git cherry-pick c2 c3
Or apply them individually:
git cherry-pick c2
git cherry-pick c3
Git will apply the changes from those commits on top of your current branch. After cherry-picking, your master branch looks like:
c0 → c5 → c6 → c2' → c3'
(The primes indicate new commit hashes, since cherry-pick creates new commits with different SHAs.)
Finding commit hashes
Use git log to find the commit hashes you need:
git log --oneline dev
This shows commit hashes and messages. Copy the hash you want to cherry-pick.
Handling merge conflicts
If cherry-pick encounters conflicts, Git will pause and let you resolve them:
# View conflicted files
git status
# Edit files to resolve conflicts
vim path/to/conflicted/file
# Mark as resolved
git add path/to/conflicted/file
# Continue the cherry-pick
git cherry-pick --continue
If you need to abort the operation:
git cherry-pick --abort
Cherry-picking a range of commits
To cherry-pick multiple consecutive commits, use a range:
git cherry-pick c2^..c3
This picks c2 and c3 (the ^.. syntax excludes c2’s parent but includes c2). Alternatively:
git cherry-pick c2..c4
This picks c3 and c4 but excludes c2. Be careful with ranges — verify which commits you’re including.
When to avoid cherry-pick
Cherry-pick is useful for targeted bug fixes, but overusing it can create maintenance headaches:
- Repeated history: If you cherry-pick the same commit to multiple branches, you’ll have duplicate logic spread across the codebase.
- Diverging branches: If dev and master drift significantly, cherry-picking becomes error-prone.
- Better alternative — merge: If you’re bringing over related features or larger changesets, merge the entire branch instead and selectively revert unwanted commits.
For ongoing development, prefer merging entire branches to keep history clean and make it easier to understand dependencies between code.
Verifying the cherry-pick
After cherry-picking, verify the changes were applied correctly:
git log --oneline -n 5
git show HEAD # View the most recent commit
Compare against the original commits on dev to ensure the code changes match what you expect.
2026 Best Practices and Advanced Techniques
For Merging a Commit from Another Git Branch, understanding both fundamentals and modern practices ensures you can work efficiently and avoid common pitfalls. This guide extends the core article with practical advice for 2026 workflows.
Troubleshooting and Debugging
When issues arise, a systematic approach saves time. Start by checking logs for error messages or warnings. Test individual components in isolation before integrating them. Use verbose modes and debug flags to gather more information when standard output is not enough to diagnose the problem.
Performance Optimization
- Monitor system resources to identify bottlenecks
- Use caching strategies to reduce redundant computation
- Keep software updated for security patches and performance improvements
- Profile code before applying optimizations
- Use connection pooling for network operations
Security Considerations
Security should be built into workflows from the start. Use strong authentication methods, encrypt sensitive data in transit, and follow the principle of least privilege for access controls. Regular security audits and penetration testing help maintain system integrity.
Related Tools and Commands
These complementary tools expand your capabilities:
- Monitoring: top, htop, iotop, vmstat for resources
- Networking: ping, traceroute, ss, tcpdump for connectivity
- Files: find, locate, fd for searching; rsync for syncing
- Logs: journalctl, dmesg, tail -f for monitoring
- Testing: curl for HTTP requests, nc for ports, openssl for crypto
Integration with Modern Workflows
Consider automation and containerization for consistency across environments. Infrastructure as code tools enable reproducible deployments. CI/CD pipelines automate testing and deployment, reducing human error and speeding up delivery cycles.
Quick Reference
This extended guide covers the topic beyond the original article scope. For specialized needs, refer to official documentation or community resources. Practice in test environments before production deployment.
