Black Friday sale: 30% off Pro and Unlimited, until 22 December

Git stash vs. WIP commit vs. a new branch

Halfway through a change and need to switch tasks? Save a named stash, leave a work-in-progress commit, or open the urgent fix in a separate worktree.

You’re halfway through adding coupons to the cart when a bug in the cart total needs a fix. The two changes touch the same file. Pick where you want to find the coupon work when you come back:

  • Stash it for a quick detour. Find the edits by name in the stash list.
  • Make a WIP commit for a checkpoint you can find in your branch history.
  • Open a new branch in a worktree when you want both tasks open in separate folders.

The examples each start on feature/cart-coupons, with the urgent fix starting from your local main. Use the base branch your team fixes bugs from.

Stash changes for a quick detour

Give the stash a name you’ll recognise tomorrow. -u includes new, untracked files, such as a test you haven’t added yet. Git leaves ignored files out of the stash. If you staged a file for deletion and then recreated it as an ignored file, move the new copy outside the checkout before you stash.

git stash push -u -m 'cart coupons'

Once Git confirms it saved your changes, start the fix:

git switch --no-overwrite-ignore \
  -c fix/cart-total main

If Git flags an ignored file on either switch, move the file somewhere safe outside the checkout before you retry.

When the fix checkout is clean, switch back and find the stash:

git switch --no-overwrite-ignore \
  feature/cart-coupons
git stash list
stash@{0}: On feature/cart-coupons: cart coupons

Match the message before you use the number. New stashes move older entries down the list. Run these checks from the repository root. The first three commands list paths saved as edits, staged changes and untracked files. The last shows ignored files in your checkout.

git diff --name-only --no-renames \
  --no-relative 'stash@{0}^1' 'stash@{0}'
git diff --name-only --no-renames \
  --no-relative 'stash@{0}^1' 'stash@{0}^2'
git stash show --only-untracked \
  --name-only --no-relative 'stash@{0}'
git status --short --ignored \
  --untracked-files=all

Git can overwrite ignored files during restore. Compare the paths marked !! with all three lists. Move matching ignored files outside the checkout, including any inside a directory the stash would replace or blocking a directory it would create. Apply the matching stash after you clear those paths.

git stash apply --index 'stash@{0}'
git status --short

--index also tries to restore what you had staged. Applying can conflict if the branch has changed. apply keeps the stash in the list either way. Once you’ve checked that your edits and new files are back, you can drop the matching entry with git stash drop 'stash@{0}'. Check the number again if the list has changed.

Make a WIP commit you can find in the log

A work-in-progress commit gives your coupon changes a place in the feature branch’s history. Stage the files you want to keep, then review everything staged before committing, including anything you staged earlier.

git status --short
git add -- cart.js cart.test.js
git diff --cached
git commit -m 'WIP: add cart coupons'
git status --short

A commit records only what was staged. Save any remaining edits you want to keep. Once the checkout is clean, start the urgent fix:

git switch --no-overwrite-ignore \
  -c fix/cart-total main

If Git flags an ignored file on either switch, move it outside the checkout for safekeeping before you retry. When the fix checkout is clean, return to the feature branch and check its recent commits:

git switch --no-overwrite-ignore \
  feature/cart-coupons
git log -3 --format=%s
WIP: add cart coupons
Add cart

Your saved work is already in the files when you switch back. Keep working from there and tidy the WIP commit according to your team’s review workflow.

Open a new branch in a separate worktree

A branch points to a commit. Creating a branch alone leaves uncommitted edits in the current checkout. If the coupon work is already committed, you can switch away and return to its branch when you’re ready. If you want to leave your edits right where they are, give the fix branch its own checkout.

Run this from the coupon checkout. Use an unused branch name and folder:

git worktree add -b fix/cart-total \
  ../web-cart-fix main
cd ../web-cart-fix

Your editor can stay open on the coupon files while you work on the fix in web-cart-fix. To find your way back from either checkout, run:

git worktree list

Open the folder listed beside [feature/cart-coupons] to resume. Its changes and staging are still there. If Git refuses a switch with “already used by worktree”, see how to fix it. Once the fix is wrapped up, see how to clean up the extra worktree.

Find the commits you left behind

A stash, an unpushed commit and edits in a worktree all live on your machine. If you need a commit on another machine, push it to a branch your team allows for unfinished work.

To see what’s still local, find your unpushed commits. If you switch between several repositories on a Mac, push& (Pushand) shows the commits waiting to be pushed from the menu bar.

More Git how-tos