Skip to main content

Stashing

When working on a Git repository, sometimes you might need to switch branches or pull updates, but you have uncommitted changes that you don’t want to commit yet. Instead of committing incomplete work, you can temporarily "stash" your changes using git stash.

Why Use git stash?

  • You need to switch branches but don’t want to commit unfinished work.
  • You want to pull the latest changes without committing temporary edits.
  • You want to keep your working directory clean for testing or debugging.

Commands

  1. git stash - Stashing Your Changes with

    Git temporarily saves (stashes) tracked changes and resets your working directory to match the last commit.

    The file is not lost; it is stored in a stash stack.

  2. git stash list - Viewing Stashed Changes

    • stash@{0}: The most recent stash.
    • WIP on main: Stashed from the main branch.
  3. git stash show -p stash@{0} - see what changes were stashed.

  4. git stash apply - This reapplies the most recent stash but keeps the stash in the list.

  5. git stash apply stash@{0} - apply specific stash.

  6. git stash pop - remove the most recent stash.

  7. git stash drop stash@{0} - remove a specific stash.

  8. git stash clear - clear all stashed.