Git & GitHub ยท Chapter 19 of 42

git stash

`git stash` temporarily saves your uncommitted changes so you can switch branches or pull updates without committing incomplete work. It's like a clipboard for work in progress.

Stashed changes are stored in a list, so you can stash multiple times and later reapply any of them with `git stash pop` or `git stash apply`.

Syntax
git stash
git stash list
git stash pop

Saving and listing stashes

Run `git stash` to save current changes and clean the working directory. Run `git stash list` to see all saved stashes.

Restoring stashed changes

Run `git stash pop` to reapply the most recent stash and remove it from the list, or `git stash apply` to reapply it while keeping it in the list.

Example 1 (bash)
git stash
Output
Saved working directory and index state WIP on main: 9c2e1aa Fix crash

Saves current uncommitted changes and restores a clean working directory.

Example 2 (bash)
git stash pop
Output
On branch main
Changes not staged for commit:
  modified:   app.js

Reapplies the most recently stashed changes and removes them from the stash list.

Key points

  • git stash temporarily saves uncommitted changes.
  • It cleans your working directory so you can switch tasks.
  • git stash pop reapplies and removes the latest stash.
  • git stash list shows all saved stashes.
๐Ÿ’ก Note: Stashes are local only โ€” they are not pushed to GitHub and can be lost if you clear your local repository.

๐Ÿ“ Quick Quiz

1. What does git stash do?

2. Which command reapplies and removes the latest stash?

3. Are stashes shared with GitHub when you push?