Synopsis
git stash push -p -m <msg> -- <pathspec>
git stash branch <newbranch> [<stash>]
git stash create
git stash store -m <msg> <sha>
Description
Beyond git stash push and git stash pop, the stash machinery offers powerful capabilities for advanced workflows. You can stash specific paths, stash specific hunks with -p, recreate a branch from a stash that conflicts with the current state, or even create a stash object without storing it on the stack (then store it later with a custom message).
Internally, each stash is a merge commit between WIP-on-branch (the working tree) and index-on-branch (the staged changes), with the parent being the branch tip. This structure is what allows stash branch and stash apply to reconstruct exact state.
Dans l'usage quotidien, git stash (advanced) s'intègre étroitement avec les alias de shell, les plugins d'éditeur et l'intégration continue. Les utilisateurs avancés ajoutent souvent des alias combinant les flags qu'ils passent toujours, ou enveloppent la commande dans des scripts qui appliquent les conventions d'équipe. Le formatage de la sortie peut être personnalisé via la configuration Git — pretty formats, schémas de couleurs et comportement du pager sont tous ajustables. Quand quelque chose tourne mal, la première étape de diagnostic est généralement de relancer la commande avec GIT_TRACE=1 dans l'environnement, ce qui révèle les appels de plomberie sous-jacents. Pour les situations inhabituelles, la sortie --help (git stash (advanced) --help) ouvre la page de manuel complète avec les détails de chaque option, y compris celles rarement utilisées dans les workflows ordinaires mais essentielles pour le débogage ou le scripting à grande échelle.
Comprendre comment git stash (advanced) interagit avec le reste du modèle de données de Git — la base d'objets, l'index, les refs et l'arborescence de travail — est rentable. Chaque commande opère sur un sous-ensemble de ces pièces, et savoir laquelle elle touche aide à prédire les résultats et récupérer après les erreurs. Lire la documentation officielle de Git en parallèle de la pratique sur un dépôt jetable est la façon la plus rapide d'intérioriser les subtilités. La plupart des problèmes de production avec Git proviennent de l'une de trois causes : comportement par défaut surprenant, opérations réseau partielles, ou réécriture d'historique déjà partagé. Un modèle mental fonctionnel des effets de bord de git stash (advanced) aide à éviter les trois.
Options courantes
| Subcommand / Option | Description |
|---|---|
push -p | Stash interactively chosen hunks. |
push -- <pathspec> | Stash only specified paths. |
--keep-index | Stash but keep what's already staged. |
--no-keep-index | Reset index when stashing (default). |
branch <name> | Recreate state on a fresh branch. |
create | Make a stash commit but don't store it. |
store -m | Place an existing commit on the stack. |
Exemples
git stash push -p -m "WIP: only refactor changes" -- src/refactor/
# Stash only specific paths, interactively
git stash --keep-index -m "Test pre-staged"
# Verify that staged changes pass tests, with WIP set aside
git stash branch fix-from-stash stash@{2}
# Resurrect a stash that no longer applies cleanly
SHA=$(git stash create "checkpoint")
git stash store -m "manual checkpoint" "$SHA"
# Programmatic stash creation
Erreurs fréquentes
Stashing during a merge or rebase mixes states and confuses the stack. Avoid. Long-running stashes outlive their context — review periodically. --keep-index is great for verifying staged changes, but the resulting stash includes both staged and unstaged content, which can surprise.
Commandes liées
git stash, git commit --fixup, git worktree, git restore