Git Stash Anleitung
Git Stash: Änderungen temporär speichern
Git Stash speichert unvollständige Arbeit, ohne zu committen. Lernen Sie, wie Sie Änderungen parken und später fortsetzen.
Grundlegende Befehle
# Änderungen stashen
git stash
# Mit Nachricht
git stash push -m "Work in progress: Feature X"
# Stash anwenden und behalten
git stash apply
# Stash anwenden und löschen
git stash pop
# Stashes auflisten
git stash list
# stash@{0}: On main: Work in progress: Feature X
# stash@{1}: WIP on main: abc1234 Previous commit
Typischer Workflow
# Situation: Du arbeitest an Feature, musst aber schnell Bug fixen # 1. Aktuelle Arbeit stashen git stash push -m "Feature: User Dashboard" # 2. Branch wechseln und Bug fixen git checkout main git checkout -b hotfix/critical-bug # ... Bug fixen ... git commit -m "Fix critical bug" git checkout main git merge hotfix/critical-bug # 3. Zurück zur Arbeit git checkout feature/dashboard git stash pop # Weiterarbeiten wo du aufgehört hast
Erweiterte Optionen
# Nur bestimmte Dateien stashen git stash push -m "Partial stash" -- file1.js file2.js # Untracked Dateien mit stashen git stash push -u -m "Include untracked" # oder git stash push --include-untracked # Auch ignorierte Dateien git stash push -a -m "All files" # oder git stash push --all # Interaktiv auswählen (Hunks) git stash push -p -m "Selected changes"
Stash anwenden
# Neuesten Stash anwenden (behalten)
git stash apply
# Neuesten Stash anwenden (löschen)
git stash pop
# Bestimmten Stash anwenden
git stash apply stash@{2}
git stash pop stash@{1}
# Stash in neuen Branch
git stash branch new-feature-branch stash@{0}
# Erstellt Branch, checkt aus, wendet Stash an, löscht Stash
Stash verwalten
# Alle Stashes anzeigen
git stash list
# Stash-Inhalt anzeigen
git stash show
# file1.js | 5 +++++
# file2.js | 3 +--
# Mit Diff
git stash show -p
git stash show -p stash@{1}
# Bestimmten Stash löschen
git stash drop stash@{0}
# Alle Stashes löschen
git stash clear
Konflikte beim Anwenden
# Wenn Konflikte auftreten git stash pop # CONFLICT: Merge conflict in file.js # Konflikt lösen # 1. Datei editieren # 2. Konfliktmarker entfernen # 3. Staging git add file.js # ⚠️ Bei pop mit Konflikt: Stash wird NICHT automatisch gelöscht! # Nach Konfliktlösung manuell löschen: git stash drop
Praktische Beispiele
# Schneller Pull mit lokalen Änderungen git stash git pull --rebase git stash pop # Änderungen in anderen Branch mitnehmen git stash git checkout other-branch git stash pop # Experimentieren ohne Risiko git stash push -m "Before experiment" # ... experimentieren ... # Wenn fehlgeschlagen: git checkout -- . git stash pop # Wenn erfolgreich: git stash drop
Stash als Patch exportieren
# Stash als Patch-Datei
git stash show -p stash@{0} > changes.patch
# Patch anwenden (auf anderem System)
git apply changes.patch
# Oder per E-Mail versenden
git stash show -p | git format-patch --stdin
Tipps und Tricks
# Alias für bessere Übersicht
git config --global alias.sl "stash list --format='%gd: %cr - %gs'"
# Stash mit Datum anzeigen
git stash list --date=relative
# Stash finden der bestimmte Datei enthält
git stash list | while read stash; do
echo "$stash"
git stash show "${stash%%:*}" 2>/dev/null | grep "filename"
done
# Letzten Drop rückgängig machen (wenn noch im Reflog)
git stash apply $(git fsck --no-reflog | awk '/dangling commit/ {print $3}')
Häufige Fehler
# ❌ Fehler: "No local changes to save" # → Es gibt nichts zu stashen (alles committed oder unverändert) # ❌ Stash in falschem Branch angewendet # → Einfach erneut stashen und Branch wechseln git stash git checkout correct-branch git stash pop # ❌ Versehentlich git stash clear # → Möglicherweise über Reflog wiederherstellbar git fsck --no-reflog | grep commit # Dann mit git show <hash> prüfen
💡 Best Practice:
Geben Sie Stashes immer beschreibende Namen mit
-m. So finden Sie später schnell den richtigen Stash wieder. Vermeiden Sie zu viele Stashes - wenn es mehr als 3-4 werden, sollten Sie echte Branches nutzen.