Skip to content
Bash

Git Stash

Stash working directory changes.

By EZ4Code Team
gitstash

Code

#!/bin/bash

# Save changes
git stash                  # Stash all tracked files
git stash -u               # Include untracked files
git stash -a               # Include .gitignore files
git stash save "WIP: feature X"  # With message (old syntax)
git stash push -m "WIP: feature X"  # With message (new syntax)

# Partial stash
git stash push -m "save partial" file1.txt file2.txt
git stash push --keep-index  # Stash but keep staging area

# View stash
git stash list
git stash show stash@{0}      # View summary
git stash show -p stash@{0}   # View full diff

# Restore stash
git stash pop              # Restore and delete latest stash
git stash pop stash@{2}    # Restore specified stash
git stash apply            # Restore but keep stash
git stash apply stash@{1}

# Delete stash
git stash drop             # Delete latest stash
git stash drop stash@{2}   # Delete specified stash
git stash clear            # Delete all stashes

# Create branch from stash
git stash branch feature-x stash@{0}

# Stash workflow
# 1. In development, need to switch branch
git stash
git checkout main
git pull
git checkout feature
git stash pop

# 2. Stash untracked files
git stash -u
# 3. Stash but keep staging area
git stash --keep-index

Explanation

git stash temporarily saves working directory changes; pop restores and deletes; apply restores but keeps; -u includes untracked files.

More Bash Snippets