git stash
git stash
Section titled “git stash”git stash temporarily shelves (stashes) changes you’ve made to the working tree so you can work on something else, then come back and restore the saved changes.
Basic Usage
Section titled “Basic Usage”# Stash uncommitted changes (tracked files only)git stash
# Stash with a descriptive messagegit stash push -m "WIP: user authentication flow"
# Also stash untracked filesgit stash push -u
# Also stash ignored files (rarely needed)git stash push --allApplying Stashes
Section titled “Applying Stashes”# Apply the most recent stash and remove it from the stash listgit stash pop
# Apply without removing from the stash listgit stash apply
# Apply a specific stashgit stash pop stash@{2}git stash apply stash@{2}Listing and Inspecting Stashes
Section titled “Listing and Inspecting Stashes”# List all stashesgit stash list# stash@{0}: WIP on feature: abc1234 Add login form# stash@{1}: On main: fix typo# stash@{2}: WIP: user authentication flow
# Show a summary of what a stash containsgit stash show stash@{0}
# Show the full diffgit stash show -p stash@{0}Managing Stashes
Section titled “Managing Stashes”# Remove a specific stashgit stash drop stash@{1}
# Remove all stashesgit stash clearCreating a Branch from a Stash
Section titled “Creating a Branch from a Stash”If a stash would conflict with your current branch, create a new branch instead:
git stash branch feature/saved-work stash@{0}# Creates a new branch at the commit the stash was made from,# applies the stash, and drops itStashing Specific Files
Section titled “Stashing Specific Files”# Stash only specific filesgit stash push -m "partial stash" path/to/file.ts
# Stage the files you want to keep, stash the restgit add important-file.tsgit stash push --keep-index -m "stash everything except staged"Common Workflow
Section titled “Common Workflow”# Working on a feature, urgent bug report arrivesgit stash push -m "WIP: feature/dashboard - half done"
# Fix the buggit switch maingit switch -c hotfix/login-crash# ... fix ...git commit -m "Fix crash on login with empty email"git switch maingit merge hotfix/login-crash
# Return to feature workgit switch feature/dashboardgit stash popStash vs Commit (WIP Commit)
Section titled “Stash vs Commit (WIP Commit)”An alternative to git stash is a “WIP commit”:
git add .git commit -m "WIP: do not merge"
# Later, undo the WIP commit (keep changes staged)git reset --soft HEAD~1WIP commits are visible in history and can be pushed to a remote branch (good for backing up work in progress). Stashes are local-only.
Gotchas
Section titled “Gotchas”- Stashes are local — they don’t push to remote. If you lose your machine, you lose your stashes
git stash popapplies the stash and deletes it. If there’s a conflict, the stash is not deleted — resolve and rungit stash dropmanually- By default,
git stashdoes not stash untracked files. Use-uto include them