objectif
Sauvegarder temporairement des modifications locales pour changer de branche, tester un patch ou repartir propre, puis réappliquer tout ou partie ensuite.
code minimal
# stash standard avec message
git stash push -m "wip: refacto api"
utilisation
# inclure les fichiers non suivis (untracked)
git stash push -u -m "wip: ajout config"
# lister les stashes avec message et base
git stash list
# voir ce que contient le dernier stash (diff)
git stash show -p
# réappliquer sans supprimer du stash (pour retester plusieurs fois)
git stash apply
# réappliquer et supprimer du stash (flux habituel)
git stash pop
# cibler un stash précis (par index)
git stash show -p stash@{2}
git stash apply stash@{2}
git stash drop stash@{2}
# créer une branche à partir d'un stash (recommandé pour gros conflits)
git stash branch feature/reprise stash@{0}
# stasher uniquement certains chemins
git stash push -m "wip: seulement src" -- src/ Makefile
# ne stasher que les changements non indexés (garder l'index pour un build/test)
git stash push --keep-index -m "wip: garde l'index"
# sélection interactive (par hunk)
git stash push -p -m "wip: patch partiel"
variante(s) utile(s)
# inclure aussi les fichiers ignorés (rare, mais utile pour caches/binaries)
git stash push -a -m "wip: inclut ignores"
# tenter de restaurer aussi l'état de l'index
git stash apply --index
# renommer "le dernier" stash (via recréation)
git stash show -p | git apply -R && git stash drop
git stash push -m "wip: nouveau message"
# exporter un stash en patch
git stash show -p stash@{0} > wip.patch
# appliquer plus tard:
git apply wip.patch
notes
- préférez
git stash pushà l’ancienne formegit stash save(dépréciée). applyconserve le stash pour d’autres usages;popréapplique puis le supprime.-u/--include-untrackedajoute les fichiers non suivis;-a/--allinclut aussi les ignorés.--keep-indexlaisse l’index intact (seuls les changements non indexés sont stashés).- en cas de conflits à l’application, résolvez-les puis
git add -Aet continuez votre flux normal.