← Back to Chapters

Git Stash

? Git Stash

? Quick Overview

Git Stash is a powerful Git feature that allows you to temporarily save uncommitted changes without creating a commit. It helps you switch branches or perform urgent fixes without losing unfinished work.

? Key Concepts

  • Stash works like a stack (Last In, First Out)
  • Supports multiple stashes
  • Can include tracked, untracked, and ignored files
  • Changes can be applied or popped later

? What is Git Stash?

Git Stash saves your current working directory changes and resets it back to the last commit. You can reapply those saved changes anytime.

? Stashing Your Changes

? View Code Example
// Save all tracked changes temporarily
git stash

? Viewing Stashed Changes

? View Code Example
// List all saved stashes
git stash list

? Applying Stashed Changes

? View Code Example
// Apply the most recent stash
git stash apply
? View Code Example
// Apply a specific stash by index
git stash apply stash@{1}

? Removing Stashes

? View Code Example
// Remove a specific stash
git stash drop stash@{0}
? View Code Example
// Remove all saved stashes
git stash clear

? Stashing Untracked & Ignored Files

? View Code Example
// Stash tracked and untracked files
git stash -u
? View Code Example
// Stash everything including ignored files
git stash -a

⚡ Apply & Remove in One Step

? View Code Example
// Apply and delete the latest stash
git stash pop
? View Code Example
// Pop a specific stash
git stash pop stash@{1}

? Interactive Concept Flow

// Working Directory → Stash Stack → Apply / Pop → Working Directory
Modified Files
      ↓
   git stash
      ↓
  Stash Stack
      ↓
git stash apply / pop

? Use Cases

  • Quickly switch branches without committing
  • Pause feature development to fix bugs
  • Clean working directory temporarily
  • Experiment safely with code changes

? Tips & Best Practices

  • Use git stash list frequently to stay organized
  • Apply stashes carefully to avoid conflicts
  • Prefer commits for long-term work

? Try It Yourself

  • Modify a file and stash it
  • Switch branches safely
  • Apply and pop stashes
  • Practice stashing untracked files