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

Terminal
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 stash
Output
Saved working directory and index state WIP on feature/dashboard: f687021 Initial site

git status now says the working tree is clean, and index.html is back to the last commit.

2.Fix the urgent bug

Terminal
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/checkout

3.Take the work back out

Terminal
git switch feature/dashboard
git stash pop
Output
On 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…”:

Terminal
echo "change 1" >> index.html
git stash -m "dashboard charts attempt"
echo "change 2" >> index.html
git stash -m "dashboard sidebar idea"
git stash list
Output
stash@{0}: On feature/dashboard: dashboard sidebar idea
stash@{1}: On feature/dashboard: dashboard charts attempt

stash@{0} is always the newest. To look inside one before using it:

Terminal
git stash show stash@{1}
Output
index.html | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Add -p to see the code changes themselves.

CommandRestores the changesRemoves the stash
git stash popYesYes
git stash applyYesNo, 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

Terminal
echo "new feature" > charts.js
git stash
Output
No local changes to save

By default, stash only saves files Git already tracks, so charts.js stays behind. -u includes new files:

Terminal
git stash -u -m "dashboard with new charts file"
FlagStashes
(none)Changed and staged tracked files
-uThose, plus new files
-aThose, plus ignored files (rarely needed)

2.Stashing only some files

Terminal
git stash push -m "only the css" styles.css   # one file
git stash push -p                             # pick changes piece by piece

3.Conflicts when popping

If the branch changed the same lines since the stash was made, pop stops with a conflict:

Output
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.

StashWIP commit
Tied to a branchNo: easy to forgetYes
Can be pushed as a backupNoYes
Best forShort interruptions, minutes to hoursLonger 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

Terminal
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 --oneline
Output
5ed23f6 (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 cart

2.Copy it to main

Terminal
git switch main
git cherry-pick 660a3bc
Output
[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:

Terminal
git branch feature/search     # new branch, with the commit on it
git reset --hard HEAD~1       # take main back one commit
git switch feature/search

If 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

CommandDoes
git cherry-pick a1b2c3d e4f5a6bCopies several commits
git cherry-pick A..BCopies everything after A, up to B (not A itself)
git cherry-pick A^..BThe same, including A (^ means “the parent of”)
git cherry-pick -x hashAdds “(cherry picked from commit …)” to the message, to trace it back
git cherry-pick -n hashStages 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 usesAvoid
An urgent fix that main needs from a feature branchMoving work between branches as a habit
A commit made on the wrong branchCopying 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 labelgit stash -u -m "message"
List stashesgit stash list
Look inside onegit stash show -p stash@{n}
Restore and removegit stash pop
Restore and keepgit stash apply stash@{n}
Stash some filesgit stash push -m "msg" file
Delete one, or allgit stash drop, git stash clear
Turn a stash into a branchgit stash branch name stash@{n}
Copy one commit heregit cherry-pick hash
Copy a range, including the firstgit cherry-pick A^..B
Record where it came fromgit cherry-pick -x hash
Copy without committinggit cherry-pick -n hash
Continue after a conflictgit cherry-pick --continue