🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

Git Stash: Vollständiger Leitfaden – Speichern, Anwenden, Pop, Drop und Branch aus Stash

⏱️5 min read  ·  1,039 words

git stashSpeichert Ihre Arbeitsverzeichnisänderungen vorübergehend, sodass Sie den Kontext wechseln können, ohne unvollständige Arbeit zu übernehmen. Es ist einer der nützlichsten, aber am wenigsten genutzten Git-Befehle. Dieser Leitfaden behandelt jeden Unterbefehl mit praktischen Beispielen.

Kernkonzept

# You're in the middle of feature work and need to switch branches
git status
# M  src/auth.ts    ← modified
# M  src/utils.ts   ← modified
# ?? src/new-file.ts  ← untracked

git checkout main  # ERROR: would overwrite changes

# git stash saves and cleans your working directory
git stash
# Saved working directory and index state WIP on feature: abc123 Add auth

git checkout main  # ✅ now clean working tree

# ... do what you needed on main ...

git checkout feature
git stash pop      # restore your changes

git stash-Befehle

# Save stash with a message (recommended — harder to forget what it was)
git stash push -m "WIP: auth middleware refactor"

# Short form (saves tracked changes only)
git stash

# Include untracked files
git stash push --include-untracked   # or -u

# Include untracked AND ignored files (rare)
git stash push --all                 # or -a

# Partial stash — choose specific files
git stash push -m "just auth changes" -- src/auth.ts src/auth.test.ts

Auflisten und Überprüfen von Lagerbeständen

# List all stashes
git stash list
# stash@{0}: WIP on feature: abc123 Add auth
# stash@{1}: WIP on main: def456 Fix bug
# stash@{2}: On feature: Manual save before experiment

# Show what's in a stash (summary)
git stash show            # most recent
git stash show stash@{1} # specific stash

# Show full diff of a stash
git stash show -p stash@{0}

Wiederherstellen: pop vs apply

# git stash pop — apply AND remove from stash list
git stash pop              # applies stash@{0} and removes it
git stash pop stash@{2}   # apply a specific stash

# git stash apply — apply but KEEP in stash list
git stash apply            # applies stash@{0}, keeps it in list
git stash apply stash@{1}

# When to use apply:
# - When you want to apply the same stash to multiple branches
# - When you're not sure the apply will be clean and want to retry
# - As a safety measure — you can always drop manually after

Konflikte beim Anwenden von Stash

# If the stash conflicts with current branch state:
git stash pop
# CONFLICT (content): Merge conflict in src/auth.ts
# The stash entry is NOT removed when conflicts occur

# Fix conflicts normally
git add src/auth.ts
git stash drop    # manually remove the stash after resolving
# (pop would have removed it automatically if no conflicts)

Von einem Stash abzweigen

# Create a new branch FROM the commit where the stash was saved
# and apply the stash to it — useful when stash won't apply cleanly to current branch
git stash branch my-new-branch stash@{0}

# This is equivalent to:
# git checkout -b my-new-branch stash-parent-commit
# git stash apply stash@{0}
# git stash drop stash@{0}

Stashes entfernen

# Remove specific stash
git stash drop stash@{0}

# Remove most recent stash
git stash drop

# Remove ALL stashes (dangerous — no undo)
git stash clear

Teilversteck mit interaktivem Modus

# Interactively choose which hunks to stash (like git add -p)
git stash push --patch    # or -p

# Shows each changed hunk and asks: stash this hunk? (y/n/d/a/e/?)
# Useful when a file has changes you want to stash and changes you want to keep

Allgemeine Entwickler-Workflows

# Workflow 1: Quick context switch
git stash push -m "WIP: new feature"
git checkout main
git pull origin main
# ... fix urgent bug ...
git checkout feature
git stash pop

# Workflow 2: Stash before risky rebase
git stash push -u -m "Before rebase backup"
git rebase origin/main
# If rebase goes wrong: git stash pop restores your state
# (though git reflog is better for full recovery)

# Workflow 3: Test clean build
git stash push -u          # stash everything including untracked
npm run build              # verify clean build works
git stash pop              # restore your work

# Workflow 4: Apply stash to different branch
git stash push -m "shared config changes"
git checkout feature-a
git stash apply stash@{0}  # apply (keep in list)
git checkout feature-b
git stash apply stash@{0}  # apply to second branch too
git stash drop stash@{0}   # now drop it

git stash vs git commit –amend vs git worktree

Szenario Bestes Werkzeug
Schneller Kontextwechsel (gleiche Maschine, 5–30 Minuten) git stash
An zwei Zweigen gleichzeitig arbeiten git worktree add
Sparen Sie WIP mit der Möglichkeit zum Pushen an Remote git commit -m "WIP" (Korrektur später)
Letztes Commit rückgängig machen, aber Änderungen beibehalten git reset HEAD~1

Häufig gestellte Fragen

F: Funktioniert Git Stash branchenübergreifend?
A: Ja. Stash ist ein globaler Stack – nicht branchenspezifisch. Sie können auf Zweig A stapeln, zu Zweig B wechseln und den Vorrat dort platzieren. Wenn die Dateien mit Zweig B in Konflikt stehen, müssen Sie einen Zusammenführungskonflikt lösen.

F: Wird Git Stash nicht verfolgte Dateien speichern?
A: Nicht standardmäßig. Verwenden Siegit stash push -u (oder--include-untracked), um neue nicht verfolgte Dateien einzuschließen. Verwenden Siegit stash push -a (oder--all), um sowohl nicht verfolgte als auch gitignorierte Dateien einzuschließen.

F: Ich habe versehentlich meinen Vorrat verloren – kann ich ihn wiederherstellen?
A: Möglicherweise. Führen Siegit fsck --no-reflog | awk '/dangling commit/ {print $3}'aus um baumelnde Commits zu finden. Danngit show <hash> um Ihren Stash-Inhalt zu identifizieren, undgit stash apply <hash> um es wiederherzustellen. Funktioniert innerhalb von ca. 30 Tagen vor der Git-Garbage Collection.

F: Wie viele Vorräte kann ich haben?
A: Keine praktische Grenze. Wenn Sie jedoch mehr als 10 Stashs haben, sollten Sie erwägen, WIP festzuschreiben oder stattdessen Git Worktree zu verwenden – eine lange Stash-Liste ist ein Zeichen für aufgeschobene Entscheidungen.

F: Kann ich nur bereitgestellte Änderungen speichern?
A: Ja:git stash push --staged speichert nur das, was sich im Index befindet (bereitgestellt), und belässt nicht bereitgestellte Änderungen im Arbeitsverzeichnis. Nützlich zum Aufteilen von Änderungen in kleinere logische Einheiten.

Fazit

git stash ist für einen sauberen Kontextwechsel unerlässlich, ohne unvollständige Arbeit zu leisten. Die Tastaturbefehle:git stash push -u -m "description" zum Speichern (auch nicht verfolgt),git stash pop zum Wiederherstellen und Entfernen,git stash apply wiederherstellen und behalten. Verwenden Siegit stash branch wenn der Stash nicht sauber auf Ihren aktuellen Zweig angewendet werden kann. Der Vorrat ist ein LIFO-Stapel – pflegen Sie ihn aktiv, löschen Sie nicht mehr benötigte Vorräte und lassen Sie nicht zu, dass er sich über 3-4 Einträge hinaus ansammelt.

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *

🌐 Read in:🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা