1 07-Stash
Emanuele edited this page 2025-12-25 13:24:44 +01:00

07 - Stash

Comandi per salvare temporaneamente modifiche non committate e ripristinarle successivamente.


Cosa è lo Stash

Lo stash è un'area temporanea dove Git salva modifiche non committate. Utile quando devi:

  • Cambiare branch con modifiche in corso
  • Fare pull senza committare lavoro incompleto
  • Provare qualcosa velocemente e tornare indietro
  • Salvare work-in-progress (WIP) temporaneamente

git stash - Salvare modifiche

Sintassi

git stash
git stash push [opzioni]
git stash save "messaggio"  # Deprecato

Esempi pratici

Stash base (salva tutto):

git stash
# oppure esplicito
git stash push

Output:

Saved working directory and index state WIP on main: abc123 Last commit message

Stash con messaggio descrittivo:

git stash push -m "WIP: implementazione login OAuth"

Stash includendo file untracked:

git stash -u
# oppure
git stash --include-untracked

Stash includendo anche file ignored:

git stash -a
# oppure
git stash --all

Stash file specifici:

git stash push -m "Partial work" src/auth.py config.ini

Stash interattivo (scegli cosa salvare):

git stash -p
# oppure
git stash --patch

Per ogni modifica chiede: stash? (y/n/q/a/d/s/e)

Quando usare stash

Scenario 1: Cambio branch urgente

# Stai lavorando su feature
git status
# modified: src/app.py (not staged)

# Bug critico su main - devi switchare
git stash push -m "WIP: feature in progress"
git switch main
# Fix bug, commit, push...

# Torna a feature e ripristina
git switch feature
git stash pop

Scenario 2: Pull con modifiche locali

# Hai modifiche locali
git pull origin main
# error: Your local changes would be overwritten

# Stash, pull, ripristina
git stash
git pull origin main
git stash pop

Scenario 3: Esperimento veloce

# Salva lavoro corrente
git stash

# Prova refactoring radicale
# (modifica codice...)

# Non funziona? Ripristina
git stash pop

git stash list - Listare stash

Sintassi

git stash list

Esempi pratici

Lista tutti gli stash:

git stash list

Output:

stash@{0}: WIP on main: abc123 Add dark mode
stash@{1}: On feature: def456 WIP OAuth implementation
stash@{2}: WIP on develop: 789abc Database migration

Lista con patch (diff):

git stash list -p

Lista con statistiche:

git stash list --stat

git stash show - Mostrare stash

Sintassi

git stash show [stash@{n}]

Esempi pratici

Mostra ultimo stash (statistiche):

git stash show

Output:

 src/app.py | 15 +++++++++------
 config.ini |  3 +++
 2 files changed, 12 insertions(+), 6 deletions(-)

Mostra stash specifico:

git stash show stash@{1}

Mostra con diff completo:

git stash show -p
git stash show -p stash@{2}

git stash pop - Applicare e rimuovere

Sintassi

git stash pop [stash@{n}]

Spiegazione

Applica lo stash al working directory e lo rimuove dalla lista stash.

Esempi pratici

Pop ultimo stash:

git stash pop

Pop stash specifico:

git stash pop stash@{1}

Gestire conflitti durante pop

git stash pop
# CONFLICT in src/app.py

# 1. Risolvi conflitti manualmente
# 2. Staging file risolti
git add src/app.py

# 3. Lo stash rimane se c'è conflitto!
git stash drop  # Rimuovi manualmente dopo risoluzione

git stash apply - Applicare senza rimuovere

Sintassi

git stash apply [stash@{n}]

Spiegazione

Applica lo stash ma lo mantiene nella lista (utile per riusare stesso stash).

Esempi pratici

Apply ultimo stash:

git stash apply

Apply stash specifico:

git stash apply stash@{2}

Apply solo modifiche staged:

git stash apply --index

Apply vs Pop

Comando Applica Rimuove da lista
git stash pop
git stash apply

git stash drop - Eliminare stash

Sintassi

git stash drop [stash@{n}]

Esempi pratici

Elimina ultimo stash:

git stash drop

Elimina stash specifico:

git stash drop stash@{1}

git stash clear - Eliminare tutti gli stash

Sintassi

git stash clear

Esempi pratici

Elimina tutti gli stash:

git stash clear

⚠️ ATTENZIONE: Operazione irreversibile!


git stash branch - Crea branch da stash

Sintassi

git stash branch <nome-branch> [stash@{n}]

Spiegazione

Crea nuovo branch dal commit dove è stato creato lo stash e applica lo stash.

Esempi pratici

Branch da ultimo stash:

git stash branch feature-from-stash

Branch da stash specifico:

git stash branch experimental stash@{2}

Quando usare

Quando lo stash crea troppi conflitti sul branch corrente:

# Stash ha molti conflitti
git stash pop
# CONFLICT everywhere!

git stash  # Re-stash

# Crea branch dedicato
git stash branch fix-conflicts
# Lavora qui senza conflitti

Workflow Avanzati

Stash parziale (file specifici)

# Salva solo alcuni file
git stash push src/app.py src/auth.py -m "Auth changes only"

# Resto dei file rimane unstashed

Stash di staged vs unstaged separati

# File staged
git add src/app.py

# Stash solo unstaged
git stash --keep-index

# src/app.py rimane staged

Riapplicare stash su branch diverso

# Stash su feature-1
git stash

# Switch a feature-2
git switch feature-2

# Applica stash di feature-1
git stash pop

Stash come backup temporaneo

# Prima di operazione rischiosa
git stash push -u -m "Backup before risky rebase"

# Fai operazione
git rebase -i HEAD~5

# Se va male
git rebase --abort
git stash pop

# Se va bene
git stash drop

Opzioni Comuni

git stash push/save

Opzione Descrizione
-u, --include-untracked Include file non tracciati
-a, --all Include anche file ignored
-m "msg" Messaggio descrittivo
-p, --patch Interattivo
--keep-index Mantiene staged area

git stash pop/apply

Opzione Descrizione
--index Ripristina anche staged area
stash@{n} Stash specifico

Best Practices

Usa messaggi descrittivi (-m)
Stash prima di switch/pull se hai modifiche
Pop/apply appena possibile (non accumulare stash)
Lista stash periodicamente (pulizia)
Non usare stash come sostituto commit
Non accumulare 10+ stash (crea confusione)


Troubleshooting

Stash non applica (conflitti):

# Opzione 1: Risolvi conflitti
git stash pop
# Risolvi, git add, git stash drop

# Opzione 2: Crea branch
git stash branch fix-stash

Recuperare stash eliminato:

# Stash sono commit! Usa reflog
git fsck --unreachable | grep commit
# Trova hash stash perso
git stash apply <hash>

🔗 Collegamenti


📝 Riepilogo comandi

# SALVARE
git stash                          # Stash tutto
git stash push -m "WIP feature"    # Con messaggio
git stash -u                       # Include untracked
git stash -p                       # Interattivo

# LISTARE
git stash list                     # Tutti gli stash
git stash show                     # Info ultimo stash
git stash show -p stash@{1}        # Diff stash specifico

# APPLICARE
git stash pop                      # Applica + rimuove
git stash apply                    # Applica senza rimuovere
git stash apply stash@{2}          # Stash specifico

# ELIMINARE
git stash drop                     # Elimina ultimo
git stash drop stash@{1}           # Elimina specifico
git stash clear                    # Elimina tutti

# ALTRO
git stash branch nome              # Branch da stash