Understanding git stash: Temporary Change Storage in Git

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"

Quick Start

Use the following commands for common stash operations:

# 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

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

Stash with description

$ git stash push -m "message"

Attaches a label to the stash for easier identification later.

Include untracked items

Captures both tracked and untracked files.

View all stashes

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

Preview stash summary

Inspect full stash differences

Reapply the latest stash

Apply and remove stash

Create a branch from a stash

$ 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

Reapply a specific stash

$ git stash apply stash@{n}

Remove a stash by index

$ git stash drop stash@{n}

Delete all stash entries

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

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

Stash with description

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

Message output: Stash saved under current branch with description.

Continue edits and stash again

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

Review stash list

Output:

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

Apply a specific older stash

$ git stash apply stash@{1}

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

Remove an applied stash

$ 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:

$ 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:

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

To clear all saved stash entries:

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:

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):

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

Commit the recovered changes:

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

Now remove another stash that is no longer required:

$ 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:

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:

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:

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:

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

Stash and Restore Untracked Files: Example

Check Your Working Directory

Example output:

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

Stash Everything Including Untracked Files

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

Ensure Your Directory Is Clean

Expected result: no pending changes or untracked files remain.

Restore the Saved Work

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.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: