Recuperare il lavoro con Reflog e Stash
Padroneggiate le reti di sicurezza di Git: usate il reflog per recuperare la situazione dopo reset e rebase errati e lo stash per mettere temporaneamente da parte e ripristinare in sicurezza il lavoro in corso durante il recupero.
Recuperare il lavoro con Reflog e Stash è una lezione Git Advanced: Monorepo, Submodules & Workflows gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Git Advanced: Monorepo, Submodules & Workflows, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Git Advanced: Monorepo, Submodules & Workflows include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Git Rarely Loses Data
It feels like a bad reset or rebase destroys work, but Git almost never truly loses commits. They become unreachable, not deleted, and remain recoverable for weeks.
The reflog is your map back to them.
What the Reflog Records
The reflog logs every move of HEAD and branch tips: commits, checkouts, resets, rebases, merges. It is local and not shared, a private diary of where your refs have been.
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# 9f8e7d6 HEAD@{1}: commit: add featureRecovering After a Bad Reset
Ran git reset --hard and lost commits? Find the pre-reset SHA in the reflog and reset back to it.
git reflog
git reset --hard HEAD@{1}Recovering After a Bad Rebase
A rebase rewrites commits. If it went wrong, the reflog still holds the pre-rebase tip. Point your branch back at it.
git reflog
git reset --hard HEAD@{5} # the entry just before 'rebase'Restoring a Single Lost Commit
You do not always want to reset the whole branch. To resurrect just one commit, cherry-pick its SHA from the reflog.
git cherry-pick 9f8e7d6The Stash as a Safety Net
Before risky recovery operations, save uncommitted work with git stash. It tucks changes away cleanly so you can experiment without losing them.
git stash push -m 'wip before recovery'Inspecting and Restoring Stashes
List stashes and bring one back. apply keeps it in the list; pop restores and removes it.
git stash list
git stash show -p stash@{0}
git stash pop stash@{0}Recovering Dropped Stashes
Dropped a stash by mistake? Stashes are commits too. Find the dangling commit and re-apply it.
git fsck --no-reflogs | grep commit
git stash apply <dangling-sha>Finding Orphans with fsck
When even the reflog does not show it, git fsck --lost-found hunts for dangling commits and blobs, the last-resort recovery tool.
git fsck --lost-foundThe Garbage Collection Clock
Unreachable objects survive until garbage collection prunes them, by default after about 30 days for reflog entries. Recover promptly; do not let weeks pass before you go looking.
The Reflog Is Local Only
Remember: the reflog lives only in your clone. A teammate's reflog will not show your moves, and a fresh clone has none. If you need shared recoverability, push branches or tags before risky operations.
Quick Check
Test your understanding of Git recovery tools.
Recap
You learned Git's safety nets: the reflog recovers from bad resets and rebases, cherry-pick restores single lost commits, the stash protects work-in-progress, fsck hunts dangling objects, and the GC clock means you should recover promptly. Git rarely loses data if you know where to look.
Domande Frequenti
La lezione «Recuperare il lavoro con Reflog e Stash» è gratuita?
Sì — il testo completo di «Recuperare il lavoro con Reflog e Stash» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Git Advanced: Monorepo, Submodules & Workflows, passa a CoddyKit PRO. Il corso Git Advanced: Monorepo, Submodules & Workflows include 4 lezioni in totale.
Cosa imparerò in «Recuperare il lavoro con Reflog e Stash»?
Padroneggiate le reti di sicurezza di Git: usate il reflog per recuperare la situazione dopo reset e rebase errati e lo stash per mettere temporaneamente da parte e ripristinare in sicurezza il lavor… Eserciti Git Advanced: Monorepo, Submodules & Workflows con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Git Advanced: Monorepo, Submodules & Workflows?
Non è richiesta alcuna esperienza precedente. Git Advanced: Monorepo, Submodules & Workflows su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Recuperare il lavoro con Reflog e Stash»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Git Advanced: Monorepo, Submodules & Workflows?
Sì. Ogni lezione Git Advanced: Monorepo, Submodules & Workflows include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Recuperare commit e branch perduti
- Debugging con Git Bisect
- Ottimizzare le prestazioni dei repository Git
- Recuperare il lavoro con Reflog e Stash