Git Stash
Stash guarda cambios temporalmente para que puedas trabajar en otra cosa y luego re-aplicarlos.
Operaciones básicas de stash
# 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
Ver 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}
Aplicar 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
Stash parcial
# 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
Administrar 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}
Consejos de stash
# 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 permiten tener múltiples directorios de trabajo en un mismo repositorio, ideal para trabajar en varias ramas a la vez.
Crear 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
Administrar 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
Estructura de directorios de worktree
# 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
└── ...
Casos de uso de worktree
# 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 con repositorio bare
# 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
| Usa Stash cuando | Usa Worktree cuando |
|---|---|
| Cambio rápido de contexto | Trabajo paralelo largo |
| Un solo conjunto de cambios | Múltiples ramas activas |
| Prefieres un solo directorio | Necesitas directorios separados |
| Pausa temporal | Desarrollo simultáneo |