Using git stash and git cherry-pick to Manage Branch Switching and Commit Transfer
This article explains how to use git stash to temporarily save uncommitted changes for safe branch switching and how to apply git cherry-pick to selectively transfer commits, helping developers avoid merge conflicts and keep their codebase clean.
In daily development, developers often encounter situations where they need to switch branches but have uncommitted changes, or accidentally merge a feature branch into a shared integration branch, causing code contamination.
For the first problem, the git stash command temporarily saves all uncommitted changes (including staged and unstaged) to a stash stack, allowing branch switching without losing work. Common commands include git stash , git stash list , git stash save 'message' , git stash pop , and git stash apply [stashName] . Untracked files can be stored with git stash -u after adding them or directly.
For the second problem, when a feature branch has been merged into a shared branch unintentionally, git cherry-pick <commit_id> can selectively transfer specific commits to another branch. Useful variations include range selections and multiple commit specifications. Options such as --continue and --abort help resolve conflicts during the cherry‑pick process.
Both git stash and git cherry-pick are local operations and do not affect the remote repository until you commit and push the changes.
The article concludes with a reminder to practice these commands and invites readers to provide feedback.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.