TechLead
Lesson 5 of 9
5 min read
Advanced Git

Stash & Worktrees

Temporarily save work with stash and work on multiple branches simultaneously with worktrees.

Git Stash

Stash temporarily shelves changes so you can work on something else, then come back and re-apply them.

Basic Stash Operations

# Stash current changes
git stash

# Stash with message
git stash push -m "Work in progress on feature"

# Stash including untracked files
git stash -u
git stash --include-untracked

# Stash including ignored files
git stash -a
git stash --all

Viewing Stashes

# List all stashes
git stash list
# Output:
# stash@{0}: WIP on main: abc1234 Last commit
# stash@{1}: On feature: def5678 Feature work

# Show stash contents
git stash show
git stash show -p  # With diff
git stash show stash@{1}  # Specific stash

# Show stash stats
git stash show --stat stash@{0}

Applying Stashes

# Apply most recent stash (keep in list)
git stash apply

# Apply and remove from list
git stash pop

# Apply specific stash
git stash apply stash@{2}
git stash pop stash@{2}

# Apply to different branch
git checkout other-branch
git stash apply

Partial Stashing

# Interactive stash (choose hunks)
git stash -p

# Stash only staged changes
git stash --staged

# Stash specific files
git stash push -m "Specific files" file1.js file2.js

# Keep index (staged changes)
git stash --keep-index

Managing Stashes

# Drop a stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

# Create branch from stash
git stash branch new-branch stash@{0}

Stash Tips

# Stash with datetime
git stash push -m "$(date +%Y-%m-%d) WIP feature"

# Find stash by message
git stash list | grep "feature"

# Show stash file list
git stash show --name-only stash@{0}

# Apply specific file from stash
git checkout stash@{0} -- path/to/file.js

Git Worktrees

Worktrees let you have multiple working directories attached to one repository, perfect for working on multiple branches simultaneously.

Creating Worktrees

# Create worktree for existing branch
git worktree add ../project-feature feature-branch

# Create worktree with new branch
git worktree add -b new-feature ../project-new-feature main

# Create worktree for specific commit
git worktree add ../project-hotfix abc1234

Managing Worktrees

# List all worktrees
git worktree list
# Output:
# /path/to/repo         abc1234 [main]
# /path/to/repo-feature def5678 [feature]

# Remove worktree
git worktree remove ../project-feature

# Force remove (if dirty)
git worktree remove --force ../project-feature

# Prune stale worktrees
git worktree prune

Worktree Directory Structure

# Recommended structure
~/projects/
├── my-repo/              # Main worktree
│   ├── .git/
│   ├── src/
│   └── ...
├── my-repo-feature/      # Feature worktree
│   ├── .git -> ../my-repo/.git/worktrees/feature
│   ├── src/
│   └── ...
└── my-repo-hotfix/       # Hotfix worktree
    └── ...

Worktree Use Cases

# 1. Quick hotfix while working on feature
git worktree add ../hotfix main
cd ../hotfix
# Fix bug, commit, push
cd ../main-repo
git worktree remove ../hotfix

# 2. Compare implementations
git worktree add ../compare-v1 v1.0.0
git worktree add ../compare-v2 v2.0.0
# Run both versions side by side

# 3. Long-running experiments
git worktree add -b experiment ../experiment main
# Work on experiment without affecting main work

Worktree with Bare Repository

# Clone as bare repository
git clone --bare https://github.com/user/repo.git repo.git

cd repo.git

# Create worktrees for different branches
git worktree add ../main main
git worktree add ../develop develop
git worktree add ../feature feature

# Structure:
# repo.git/          (bare repo, no working files)
# main/              (worktree for main)
# develop/           (worktree for develop)
# feature/           (worktree for feature)

Stash vs Worktree

Use Stash When Use Worktree When
Quick context switch Long parallel work
Single set of changes Multiple active branches
Same directory preferred Need separate directories
Temporary pause Simultaneous development

Continue Learning