Stash | Tools - Wyatt's Notes
flowchart TD
A[02 Stash] --> B[Key Concepts]
A --> C[Core Principles]
A --> D[Practical Applications]
B --> E[Fundamental definitions]
C --> F[Design patterns]
D --> G[Real-world usage]What is Stash
Section titled “What is Stash”git stash temporarily shelves changes in your working directory and index, restoring your repository to a clean state (matching HEAD). It is a stack-based mechanism, you can push multiple stashes and pop them in LIFO order.
When to Use Stash
Section titled “When to Use Stash”- Switching branches with uncommitted work you cannot commit yet.
- Pulling remote changes that conflict with your local work.
- Running a quick test on a clean working directory.
- Context-switching between tasks.
Common Commands
Section titled “Common Commands”git stash # Stash all modified tracked filesgit stash -u # Stash including untracked filesgit stash -m "WIP: auth" # Stash with a descriptive messagegit stash list # Show all stashesgit stash pop # Apply and remove most recent stashgit stash apply # Apply without removinggit stash drop # Remove most recent stashgit stash clear # Remove all stashesgit stash show -p # Show diff of most recent stashCommon Pitfalls
Section titled “Common Pitfalls”- Forgetting stash is branch-specific: Stashes live in the repository, not on a branch. You can apply a stash from any branch, but merge conflicts may arise if the working tree has diverged.
- Stashing dirty state: Stash only saves tracked file changes. Untracked files require
git stash -u. New files added after a stash are not captured. - Losing stashes:
git stash clearis destructive and irreversible. Usegit stash listbefore clearing to verify what you are discarding.
Summary
Section titled “Summary”git stash temporarily shelves changes in your working directory, restoring the repository to a clean state matching HEAD. It uses a stack-based mechanism (LIFO order) for multiple stashes. Key commands include git stash (create), git stash pop (apply and remove), git stash apply (apply without removing), and git stash list (show all stashes). Use -u to include untracked files and -m for descriptive messages. Stashes are stored in the repository, not on a branch, so they can be applied from any branch but may cause merge conflicts if the working tree has diverged.
Worked Examples
Section titled “Worked Examples”Example 1: Quick Task Switch
Section titled “Example 1: Quick Task Switch”You are working on feature A but need to fix a bug on main.
git stash -m "WIP: feature A"git checkout main# fix the bug, commit, pushgit checkout feature-branchgit stash popExample 2: Stashing Untracked Files
Section titled “Example 2: Stashing Untracked Files”You have new files that are not yet tracked by Git.
git stash -u -m "Include new files"# Now working directory is clean, including untracked filesgit stash popCross-References
Section titled “Cross-References”- Git Fundamentals - Basic Git commands
- Git Branching - Branch management
Advanced Content
Section titled “Advanced Content”This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.
Derivations and Proofs
Section titled “Derivations and Proofs”Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.
Extended Examples
Section titled “Extended Examples”Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.
Research Connections
Section titled “Research Connections”This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.
Prerequisites
Section titled “Prerequisites”Ensure you have mastered the prerequisite material before attempting this advanced content.