Git and GitHub · Part 2 · Branches and merging
Stash and cherry-pick
Two small, everyday tools
- Stash puts half-finished work aside for a moment.
- Cherry-pick copies one commit from another branch.
git stash: a drawer for unfinished work
Halfway through a dashboard on feature/dashboard, with nothing working yet, a message arrives: the checkout page is broken on the live site. The work can't be committed (it's broken), switching may be blocked by Git, and it shouldn't be thrown away.
1.Put the work in the drawer
cd ~/Desktop
mkdir stash-lab
cd stash-lab
git init
echo "<h1>Store</h1>" > index.html
echo "<h1>Checkout</h1>" > checkout.html
git add .
git commit -m "Initial site"
git switch -c feature/dashboard
echo "<h1>Dashboard</h1>" > index.html
echo "<div>charts here...</div>" >> index.html
git stashSaved working directory and index state WIP on feature/dashboard: f687021 Initial sitegit status now says the working tree is clean, and index.html is back to the last commit.
2.Fix the urgent bug
git switch main
git switch -c hotfix/checkout
echo "<button>Pay Now</button>" >> checkout.html
git commit -am "Fix missing pay button on checkout"
git switch main
git merge hotfix/checkout3.Take the work back out
git switch feature/dashboard
git stash popOn branch feature/dashboard
Changes not staged for commit:
modified: index.html
Dropped refs/stash@{0} (0f559e506b426794ddf1b92d35f8f24c85a560c9)The half-finished dashboard is back, exactly as it was.
Several stashes
The drawer holds a stack of stashes, newest on top. Give each a message with -m, or a week later it's just “WIP on…”:
echo "change 1" >> index.html
git stash -m "dashboard charts attempt"
echo "change 2" >> index.html
git stash -m "dashboard sidebar idea"
git stash liststash@{0}: On feature/dashboard: dashboard sidebar idea
stash@{1}: On feature/dashboard: dashboard charts attemptstash@{0} is always the newest. To look inside one before using it:
git stash show stash@{1}index.html | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)Add -p to see the code changes themselves.
| Command | Restores the changes | Removes the stash |
|---|---|---|
git stash pop | Yes | Yes |
git stash apply | Yes | No, it stays in the list |
Both use the newest stash unless given one, as in git stash apply stash@{1}. apply is handy for using the same changes on several branches. git stash drop stash@{1} deletes one stash; git stash clear deletes all of them, and they're hard to get back.
Stash gotchas
1.New files aren't stashed
echo "new feature" > charts.js
git stashNo local changes to saveBy default, stash only saves files Git already tracks, so charts.js stays behind. -u includes new files:
git stash -u -m "dashboard with new charts file"| Flag | Stashes |
|---|---|
| (none) | Changed and staged tracked files |
-u | Those, plus new files |
-a | Those, plus ignored files (rarely needed) |
2.Stashing only some files
git stash push -m "only the css" styles.css # one file
git stash push -p # pick changes piece by piece3.Conflicts when popping
If the branch changed the same lines since the stash was made, pop stops with a conflict:
CONFLICT (content): Merge conflict in index.html
The stash entry is kept in case you need it again.The stash isn't deleted, so nothing is lost. Resolve it as in Lesson 12, then git stash drop once it's right. If a stash keeps conflicting, or has grown into real work, git stash branch feature/sidebar stash@{0} makes a new branch from the commit where it was stashed, applies it there without conflicts, and drops it.
Stash or a WIP commit?
Some developers commit half-done work as WIP instead, and clean it up later with git reset --soft HEAD~1 or a fixup.
| Stash | WIP commit | |
|---|---|---|
| Tied to a branch | No: easy to forget | Yes |
| Can be pushed as a backup | No | Yes |
| Best for | Short interruptions, minutes to hours | Longer breaks, or overnight |
git cherry-pick: copy one commit
A teammate's feature/payments branch has a commit, “Fix currency rounding bug”, that main needs now, but the rest of the feature isn't ready. Merging the branch would bring everything. git cherry-pick copies just that one commit onto the current branch, like photocopying one page of someone else's notebook.
1.A branch with one commit worth taking
cd ~/Desktop
mkdir cherry-lab
cd cherry-lab
git init
echo "price = 10.0" > cart.js
git add cart.js
git commit -m "Initial cart"
git switch -c feature/payments
echo "stripe setup" > payments.js
git add payments.js
git commit -m "Add Stripe setup (not ready)"
sed -i '' 's/10.0/10.00/' cart.js
git commit -am "Fix currency rounding bug"
echo "more stripe" >> payments.js
git commit -am "Add Stripe webhook (not ready)"
git log --oneline5ed23f6 (HEAD -> feature/payments) Add Stripe webhook (not ready)
660a3bc Fix currency rounding bug ← only this one
4d2bf9b Add Stripe setup (not ready)
5357500 (main) Initial cart2.Copy it to main
git switch main
git cherry-pick 660a3bc[main 24a9114] Fix currency rounding bug
Date: Mon Nov 23 19:21:41 2026 -0500
1 file changed, 1 insertion(+), 1 deletion(-)main now has the fix (price = 10.00) and no payments.js. The copy has a new hash, 24a9114, because its parent is different: it's a copy, not a move.
A commit on the wrong branch
A commit meant for feature/search went onto main. If the branch doesn't exist yet, create it right there, so it includes the commit, then move main back:
git branch feature/search # new branch, with the commit on it
git reset --hard HEAD~1 # take main back one commit
git switch feature/searchIf the branch already exists, copy the commit over first: git switch feature/search, git cherry-pick <hash>, then go back and reset main.
More cherry-pick options
| Command | Does |
|---|---|
git cherry-pick a1b2c3d e4f5a6b | Copies several commits |
git cherry-pick A..B | Copies everything after A, up to B (not A itself) |
git cherry-pick A^..B | The same, including A (^ means “the parent of”) |
git cherry-pick -x hash | Adds “(cherry picked from commit …)” to the message, to trace it back |
git cherry-pick -n hash | Stages the changes without committing, to adjust or combine them |
Conflicts work the same as everywhere else: fix the file, git add, then git cherry-pick --continue, or --abort to cancel (--skip drops one commit when picking several). It's the pattern merge, rebase, revert and stash all share:
fix the file → git add → --continue (or --abort)When to cherry-pick
A cherry-pick puts the same change in the repository twice, under two hashes. Git usually handles that when the branches are merged later, but not always.
| Good uses | Avoid |
|---|---|
| An urgent fix that main needs from a feature branch | Moving work between branches as a habit |
| A commit made on the wrong branch | Copying dozens of commits; merge or rebase instead |
| One useful commit from an abandoned branch | |
| Bringing a fix back to an older release branch |
Quick reference
| To… | Run |
|---|---|
| Stash everything, with a label | git stash -u -m "message" |
| List stashes | git stash list |
| Look inside one | git stash show -p stash@{n} |
| Restore and remove | git stash pop |
| Restore and keep | git stash apply stash@{n} |
| Stash some files | git stash push -m "msg" file |
| Delete one, or all | git stash drop, git stash clear |
| Turn a stash into a branch | git stash branch name stash@{n} |
| Copy one commit here | git cherry-pick hash |
| Copy a range, including the first | git cherry-pick A^..B |
| Record where it came from | git cherry-pick -x hash |
| Copy without committing | git cherry-pick -n hash |
| Continue after a conflict | git cherry-pick --continue |