Tutorials  /  Linux Basics

How to Use Git Stash: Save & Restore Code Safely

Ccentron Redaktion · August 2025 ·9 min read ·Linux Basics, Tutorial

The git stash command allows developers to temporarily shelve uncommitted modifications in their working directory. This is especially helpful when needing to switch to another branch or task without finalizing current work. Upon execution, Git captures both staged and unstaged changes, storing them in a stack and clearing them from the working copy for later use.

This guide walks through how to utilize git stash to preserve progress, retrieve or discard stashes, include files not yet tracked, and effectively handle multiple saved states.

Comparison: git stash vs git commit

Feature Git Stash Git Commit
Purpose Temporarily preserves changes without committing Permanently registers changes in the repository
Usage Scenario When pausing work mid-task or switching branches When work is finalized and ready for version history
Affects History No impact Yes – adds an entry to the project history
Includes Untracked Files Optionally with flags such as --include-untracked Yes, once added using git add
Recovery Can be re-applied or discarded Can revert or reset, but is permanently recorded
Typical Command git stash git commit -m "message"
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Quick Start

Use the following commands for common stash operations:

Console
# Save all current changes
$ git stash

# Stash with a label
$ git stash push -m "Work in progress"

# Display all stashes
$ git stash list

# Restore and remove the latest stash
$ git stash pop

# Remove a specific stash entry
$ git stash drop stash@{1}

# Delete all stash entries
$ git stash clear

Syntax Overview

git stash [OPTIONS]

  • save: Records staged and unstaged modifications in the stash stack.
  • pop: Reapplies the latest stash and deletes it from the list.
  • apply: Reapplies the latest stash but keeps it in the stash list.
  • list: Shows all saved stashes in the repository.
  • show: Presents summary or diff for latest or specific stash.
  • drop: Deletes a specified stash.

Essential git stash Commands

Store all pending modifications

Console
$ git stash

This command saves all changes and resets the directory to the last commit.

Stash with description

Console
$ git stash push -m "message"

Attaches a label to the stash for easier identification later.

Include untracked items

Console
$ git stash -u

Captures both tracked and untracked files.

View all stashes

Console
$ git stash list

Lists all stashes using identifiers like stash@{0}.

Preview stash summary

Console
$ git stash show

Inspect full stash differences

Console
$ git stash show -p

Reapply the latest stash

Console
$ git stash apply

Apply and remove stash

Console
$ git stash pop

Create a branch from a stash

Console
$ git stash branch <branch-name>

Explanation of Common Flags

  • -u or --include-untracked: Captures untracked files along with tracked ones.
  • -m: Assigns a message when saving a stash for clarity.
  • -p: Activates interactive mode for selecting specific changes to stash.

Working with Multiple Stashes

Developers often stash numerous changes when juggling multiple tasks. Git organizes stashes in a stack-like system, labeled using stash@{n}, with n = 0 being the most recent. The following instructions explain how to view, apply, and clean up specific entries.

Display all stash entries

Console
$ git stash list

Reapply a specific stash

Console
$ git stash apply stash@{n}

Remove a stash by index

Console
$ git stash drop stash@{n}

Delete all stash entries

Console
$ git stash clear

Warning: The git stash clear command removes all saved stashes permanently and cannot be reversed. Always inspect your list before using it.

Example Workflow

To demonstrate stash usage, follow these steps:

Make file changes

Console
$ echo "<h1>Header</h1>" > index.html
$ echo "console.log('Debug');" > app.js

Stash with description

Console
$ git stash push -m "UI update: Header and debug script"

Message output: Stash saved under current branch with description.

Continue edits and stash again

Console
$ echo "<footer>Footer</footer>" >> index.html
$ git stash push -m "Added footer component"

Review stash list

Console
$ git stash list

Output:

  • stash@{0}: Added footer component
  • stash@{1}: UI update: Header and debug script

Apply a specific older stash

Console
$ git stash apply stash@{1}

The changes from stash@{1} are restored but remain listed.

Remove an applied stash

Console
$ git stash drop stash@{1}

Output confirms deletion of specified stash.

Clean Up Your Git Stash

Over time, your list of stashes can grow as you save changes for different tasks or features. To keep your development environment tidy and avoid confusion, Git offers commands to remove individual stashes or clear the entire stack. This section explains how to tidy up your stash entries and provides a safe example workflow.

Command Reference

To delete a specific stash:

Console
$ git stash drop stash@{n}

This command deletes the stash at the given index stash@{n}, leaving other stashes untouched.

To remove the most recent stash entry:

Console
$ git stash drop

If you omit the index, Git will delete stash@{0} by default.

To clear all saved stash entries:

Console
$ git stash clear

Warning: git stash clear permanently deletes all stashes and cannot be undone. Always check your list before using this command.

Example Cleanup Workflow

Follow this process to apply, commit, and remove stash entries in a clean way:

First, check your existing stashes:

Console
$ git stash list

Example output:

  • stash@{0}: On main: Recent component styling
  • stash@{1}: On main: API endpoint integration
  • stash@{2}: On dashboard-feature: Error handling fix

Apply and delete the third stash (index 2):

Console
$ git stash pop stash@{2}

This applies stash@{2} and removes it from the list.

Commit the recovered changes:

Console
$ git commit -am "Fix error handling in authentication flow"

Now remove another stash that is no longer required:

Console
$ git stash drop stash@{1}

Note: After a stash is deleted, all subsequent entries are renumbered. For example, stash@{2} becomes stash@{1} after removing the previous one.

To remove all remaining stash entries:

Console
$ git stash clear

Stashing Untracked or Ignored Files

By default, Git only stashes modifications to files it already tracks. If you want to stash new or ignored files as well, use specific flags to include them in your stash.

Include Untracked Files

To stash both tracked and new files:

Console
$ git stash push -u

This saves all changes including files not yet staged or committed.

Include Everything (Ignored Files Too)

To stash everything — even files listed in .gitignore — use:

Console
$ git stash push -a

Warning: This includes dependencies, build outputs, and other ignored files. Use this option carefully.

Inspect a Stash with Untracked Files

To view the full diff of a stash containing untracked files:

Console
$ git stash show -p -u stash@{0}

Stash and Restore Untracked Files: Example

Check Your Working Directory

Console
$ git status

Example output:

  • user-profile.html is modified
  • login-validation.js is untracked

Stash Everything Including Untracked Files

Console
$ git stash push -u -m "WIP: Auth validation script"

Ensure Your Directory Is Clean

Console
$ git status

Expected result: no pending changes or untracked files remain.

Restore the Saved Work

Console
$ git stash pop

Example output:

  • Modified file: user-profile.html
  • Untracked file: login-validation.js
  • Stash reference dropped: refs/stash@{0}

With these flags and steps, you can securely stash your entire working state — including files not tracked by Git — and bring them back without loss.

Final Thoughts

Throughout this guide, you’ve explored how git stash enables you to preserve ongoing work, manage multiple entries, handle untracked or ignored files, and resolve common challenges. Whether you're pausing development or preparing a clean slate for new work, stashing empowers you to stay flexible without committing half-finished code.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren