World map

 

BM: Baca dalam Bahasa Melayu

World 3: Parallel Universes

Put work aside with stash

You will learn: to park half-done work, switch branches, and get it back.

The problem

bash
git switch main
error: Your local changes to the following files would be overwritten by checkout:
style.css
Please commit your changes or stash them before you switch branches.
Aborting
git switch main fails: your local changes to style.css would be overwritten by checkout. Please commit your changes or stash them before you switch branches.

Dark mode is half done. Then: an urgent fix on main! Git won’t let you switch. Now what?

Watch

style.css, with unfinished changes, leaves your folder and goes into a drawer. Your folder is clean.

git stash puts unfinished changes in a drawer. Your folder is clean again, so you can switch. The drawer is the stash: A drawer for unfinished changes. Your folder gets clean; take them back later..

Try it

Put your work in the drawer. Then switch to main.

Working folder
Staging area
  • Empty
Repository
  • 4 save points
  • Start dark mode
  • Add style
  • Add day 1 page

Watch

git stash saves the work. git stash pop puts it back: style.css shows as modified, and the stash entry is dropped. Terminal output: $ git stash Saved working directory and index state WIP on dark-mode: 2e1b6b1 Start dark mode $ git stash pop On branch dark-mode Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: style.css no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (019f9dcc44c3bbf75b2a3c793747f47c00b90299)

Done with the fix? git stash pop takes the newest work out of the drawer. It lands in your folder again.

Try it

You fixed main. Go back to dark-mode and take your work out of the drawer.

Working folder
  • Empty
Staging area
  • Empty
Repository
  • 4 save points
  • Fix home page title
  • Add style
  • Add day 1 page

Predict

You stash twice, then pop once. Which work comes out?

Pick one answer.

Check

Here is a drawer with two entries.

Tap the entry that git stash pop takes out.

~/diary $ git stash list

0 of 1 found

You did it!

You can now put work aside and get it back later.

Go deeper (optional)

git stash -u also takes new, untracked files. git stash push -m "note" adds a label. git stash apply puts work back but keeps a copy in the drawer. git stash drop throws one away for good. A pop can clash with newer changes. Then the markers say Updated upstream and Stashed changes.