Version control would be nearly useless without the ability to seamlessly share code between branches. Thankfully the ubiquitous Git toolset offers developers a killer feature for surgically propagating changes: cherry-picking. In the right hands, it proves invaluable for streamlining collaboration, accelerating workflows, and taming unwieldy commit histories. However mastering this skill requires examining how cherry-picking capabilities extend beyond basic merging.
Especially when engaged in large features spanning multiple devs and repos, precise control over change integration creates order from potential chaos. Much of this control manifests itself through astute utilization of Git’s beloved yet sometimes mystifying ‘cherry-pick’ command. In this extensive guide, you will gain a comprehensive overview of how to expertly leverage cherry-picking to painlessly weave desired functionality into your stacks without adding superfluous noise.
Cherry Picking Overview
In simple terms, cherry-picking allows selectively applying commits from one branch into another. This incorporates only the exact changes you want rather than bundling everything together through conventional merging.
But what gives this technique its coveted flexibility lies in customizing what parts of a commit to transfer:
- The modifications themselves
- Metadata like dates and authors
- Full commit creation with changelog
Deliberately incorporating changes without their associated commit bubbles enables seamlessly moving vital bits of code between collaborative works while still allowing independent histories. Though this does demand more familiarity with lower level Git workings, being able to avoid chains of rat’s nest merge commits pays dividends for all developers, from part timers to elite troubleshooters squeezing out optimizations from towering legacy applications.
Especially by leveraging the almighty yet oft overlooked --no-commit option, keepers of the commit log can mix specialized fixes and features into multiple dependent stacks quicker than seemingly possible. Let us explore exactly why this seemingly niche switch deserves recognition as a crown jewel shining brighter after each successful build.
The Allure of No-Commit Cherry Picking
Though most graphical UI wrappers gently nudge users away from directly interfacing Git’s intricacies, much power dwells in its lesser tread trenches. The ability to apply modifications explicitly without formally committing them serves as Excalibur allowing the savviest developers to cleave repositories perfectly aligned to their will.
But before covering how to wield this refinement technique, we must understand the exact problems it aims to solve for us.
Why Avoid Committing on Cherry Picks?
When initially trying out Git, most users find Nirvana attained simply interacting through adds, commits, pushes and pulls, never peering under the hood at lower-level operations chugging along driving it all. And fair enough, you can certainly build an entire career relying solely on these simple abstractions.
However, for truly advanced application building, especially regarding shipping experimental or temporary changes on large, fast-moving projects, judiciously avoiding formal commits while still transplanting previous work inserts magic into workflows.
Let us examine the types of common scenarios where the no-commit option brings cheer to lives that might otherwise descend into toil:
Prototyping and Spikes
When tasked with proving feasibility of high risk enhancements with short feedback loops, you want changes viewable for demonstration as soon as possible without persisting half-baked trials once done.
Patch Reviews
While examining colleague submissions or pull requests, directly build on top of work-in-progress without muddying immutable histories if abandoning direction.
Safely Debugging
Instrument code, experiment with remedies, produce artifacts vital for pinpointing defects without injecting obvious temporary code into permanent shared records.
Reusing Throwaway Fixes
Why perfectly clone git-blame detectable one-off fixes implemented earlier when collaborating with other stacks expecting entirely discrete histories?
Incorporating Feedback
Rapidly iterate on reviews, umbrella tasks and creative visions from product owners without confusing downstream change traces.
As evidenced above, the no-commit option essentially permits using cherry-pick as a mutable clipboard between branches rather than permanent merging tool. Just as invaluable hot reloading and inspection points ease daily coding cycles, think of this as source control hot swapping.
Next we will walk step-by-step through putting these big picture concepts into immediate action.
Utilizing Cherry Picks Without Committing
Thankfully, while extremely useful, avoiding commit creation on cherry picking requires no arcane incantations. Here is a quick recipe for getting modifications propagating between branches without additional commits:
Prerequisites
- Two distinct branches, one containing desired commits, another ready to incorporate changes
- Basic Git / command line literacy
Instructions
- Checkout target import branch where you’ll import the changes
git checkout my-target-branch
- Locate commit(s) with needed changes in source branch
git log --oneline
- Cherry pick changes using
-nor--no-commitgit cherry-pick -n <commit_id>
- Review and test imported modifications
- Commit changes as usual once satisfied
And that is all there is to it! The key points being:
- Use
-n/--no-commitwhen cherry-picking - Modifications will be applied to working directory without new commit
- Allows reviewing changes before finally committing
Now that you understand the basics, let us dive into richer examples demonstrating nuances of adopting this workflow.
Walkthrough of Common Cherry Picking Without Committing Use Cases
While the fundamental concepts remain identical across applications, intricacies emerge applying these tools effectively scaling up to large enterprises. To better illustrate adapating no-commit cherry picking appropriately, we will step through two ubiquitous scenarios.
Copying Throwaway Fixes
A quick illustration of appropriately reusing commits without dragging along history appears when needing to resolve defects in disparate but co-dependent components.
Say we have an frontend application, storefront, that recently underwent large refactors for performance gains. Though mostly successful, some edge case regressions slipped in only rarely visible to users. Concurrently the checkout microservice relied upon by storefront requires emergency fixes for financial accuracy.
Later on, the original developer of storefront returns and wants to quickly confirm a fix they committed earlier in checkout post-deployment also applies cleanly in new rebuilt frontend before dedicating further time debugging.
Rather than formally merging universal fixes and growing chains of difficult to interpret commits referencing others branches, they can safely test out changes by:
# Hack commits on checkout earlier
git checkout checkout-team/bugfix-shipping-totals
...fix bug and commit...
git checkout my-new-storefront
git cherry-pick -n <patch_commit_id>
# Test changes, all looks good!
git commit
By doing so, they rapidly validate portable resolutions without entangling commit lineages across teams. This further allows effortlessly reverting or iterating on changes by simply resetting commits once investigated rather than unwinding merges.
Reviewing Pull Requests
Another area this shines appears when analyzing work-in-progress changes submitted by others before integrate into critical paths.
Whether by email patch or GitHub pull request, it proves extremely efficient to spin up local versions of target environment branches with others changes layered in without create permanent copies each experiment.
For example, a senior engineer could locally simulate a team member‘s in progress checkout overhaul by:
# New member fork
git remote add dev-Bob https://github.com/BobEngineer/checkout
git fetch dev-Bob
git checkout -b simulate-bobs-work dev/main
git cherry-pick -n dev-Bob/checkout-refactor
Now upstream changes appear built atop the standard branch with the safety of tossing all changes should they not pass review or manual testing without communicating this back to the author. The engineer avoided distracting the team member with constant formal rejection of pull requests while still rapidly iterating. This ultimately allows more ideas reached by all collaborators.
Recipes for Automating Cherry Picks
While interactively selecting commits proves useful for one-off investigations, larger shops demand scripting up common routines to reduce mental toll. Especially when integrating temporary staging branches into shared playground environments, eliminating any unnecessary manual commands and adopting consistent conventions pays dividends.
For example, a continuous integration pipeline could automatically compile experimental work from feature teams into a shared preview stack by:
# Fetch all remote changes
git fetch --all
# Checkout demo stack
git checkout experiment-integration
# Reset any existing changes
git reset --hard origin/main
# Cherry pick all commits from a team not
# already found on the main branch
git cherry-pick -n `git rev-list --right-only --no-merges team-alpha/new-feature`
# Build and deploy current state of repo
...
This allows that team to always view work rendered in a sandbox environment matching the core platform without needing peer reviews and promotions upstream until sufficiently polished.
Scaling No-Commit Cherry Picks in the Enterprise
As demonstrated, delaying permanent application of changes allows smoothly influencing multiple dependent efforts in isolation; enabling easier experimentation, reviews and integration for both new and established teams. This proves invaluable from early prototyping through to continuous delivery.
However, while these tools provide sought after flexibility, take care not to dismiss all existing processes and constraints in environments with many concurrent moving parts and stakeholders. Brute force committing directly to all shared branches risks hurting velocity long term even when solving immediate problems.
With judicious balancing of automation and communication, low-commit cherry picking can accelerate systems of all scales up to the largest enterprises. But first ensure your particular company‘s culture and expectations permit fully realizing this speed.
Now that you have a solid conceptual grasp and common starter recipes for no-commit cherry picks, go forth and let this unlock cleaner collaboration and happier teams!


