Git Stash Explained (Save Local Changes Without Commit + Examples)

Git Stash Explained (Save Local Changes Without Commit + Examples)

What is Git Stash?

Git stash is used to temporarily save your local changes without committing them. This allows you to clean your working directory so you can switch branches, pull updates, or perform other operations without losing your work.

For example, if you are working on a feature and suddenly need to switch to another branch:

bash
git stash
git checkout another-branch

👉 Your changes are safely stored and can be restored later.

In simple terms:

Git stash = temporarily store your work without committing

What does git stash do?

When you run git stash, Git:

  • Saves your modified tracked files (and optionally staged changes)
  • Reverts your working directory back to the last committed state
  • Stores your changes in a stack-like structure (called "stash")

You can later:

  • View stashed changes
  • Apply them back
  • Remove them if no longer needed

👉 Think of it as a temporary clipboard for your code changes

When should you use git stash?

Use git stash when:

  • You want to switch branches without committing unfinished work
  • You need to pull latest changes but have local modifications
  • You want to temporarily save work-in-progress changes
  • You want a quick way to backup changes without creating commits

Avoid using stash when:

  • You need permanent history (use commit instead)
  • You are collaborating heavily and changes must be visible

Git Stash – Quick Cheat Sheet

DescriptionCommand
Stash current changesgit stash
Stash with messagegit stash push -m "message"
Stash only staged changesgit stash --staged
Stash including untracked filesgit stash -u
Stash including ignored filesgit stash -a
Stash specific filesgit stash push <file>
Stash all local changesgit stash -u
List all stashesgit stash list
Show stash detailsgit stash show
Show full diff of stashgit stash show -p
Apply latest stashgit stash apply
Apply specific stashgit stash apply stash@{n}
Apply and remove stashgit stash pop
Pop specific stashgit stash pop stash@{n}
Create branch from stashgit stash branch <branch-name>
Drop latest stashgit stash drop
Drop specific stashgit stash drop stash@{n}
Clear all stashesgit stash clear
Check stash statusgit stash list
Stash before pullgit stash && git pull && git stash pop
Recover dropped stashgit reflog
Save changes without commitgit stash
Restore stashed changesgit stash apply
Remove stash after applygit stash drop
Stash changes before switching branchgit stash
View stash file listgit stash show --name-only

Quick Examples

bash
# Save local changes
git stash

# Save with message
git stash push -m "work in progress"

# List stashes
git stash list

# Apply stash
git stash apply

# Apply and remove stash
git stash pop

# Stash including untracked files
git stash -u

Common Git Stash Commands

git stash

The git stash command is used to temporarily save your local changes without committing them.

bash
git stash

This command stores your changes and resets your working directory to the last committed state. It is commonly used when you want to switch branches without committing incomplete work, similar to workflows explained in git checkout command.

git stash list

To view all saved stashes:

bash
git stash list

This displays a list of stashes in the format:

text
stash@{0}: WIP on branch-name
stash@{1}: WIP on another-branch

git stash show

To view details of a stash:

bash
git stash show

For full changes (diff):

bash
git stash show -p

This helps you inspect what was saved before applying it, similar to inspecting changes using git diff examples.

git stash apply

To restore stashed changes without removing them:

bash
git stash apply

To apply a specific stash:

bash
git stash apply stash@{1}

This keeps the stash in the list for future reuse.

git stash pop

To apply and remove the stash at the same time:

bash
git stash pop

This is the most commonly used way to restore changes after temporarily saving them.


Stash Local Changes

Stash tracked files

By default, git stash only saves tracked (modified) files:

bash
git stash

This is useful when you want to temporarily save your progress before pulling updates or switching branches.

Stash all changes (including untracked)

To stash both tracked and untracked files:

bash
git stash -u

To include ignored files as well:

bash
git stash -a

This ensures that all local changes are saved, including newly created files.


View and Manage Stashes

List stashes

To see all saved stashes:

bash
git stash list

Each stash is indexed and can be referenced later.

Show stash details

To inspect what changes are stored in a stash:

bash
git stash show -p

This gives a detailed diff view, helping you decide whether to apply or discard the stash.

Delete stash

To remove a specific stash:

bash
git stash drop stash@{0}

To remove all stashes:

bash
git stash clear

👉 Use this carefully, as deleted stashes are not easily recoverable without using advanced tools like git reflog tutorial.


Apply or Restore Stash

Apply or restore stash

To restore the most recent stash without removing it:

bash
git stash apply

This reapplies your changes but keeps the stash saved for reuse. This is useful when you want to test changes without losing the backup.

Apply or restore specific stash

If you have multiple stashes, you can apply a specific one:

bash
git stash apply stash@{1}

You can identify the correct stash using git stash list.

Pop stash vs apply

  • git stash apply → restores changes but keeps stash
  • git stash pop → restores changes and removes stash
bash
git stash pop

👉 Use pop when you no longer need the stash, and apply when you may reuse it.

Create a new branch from stash

If you want to apply a stash directly to a new branch, use:

bash
git stash branch <branch-name>

Example:

bash
git stash branch feature-login

This command:

  • creates a new branch
  • checks out that branch
  • applies the latest stash
  • removes the stash if applied successfully

👉 This is useful when you started making changes on the wrong branch and later decide those changes should go into a separate branch. In such cases, it also works well with git branch examples and git switch command.

If you have multiple stashes, first identify the correct one using:

bash
git stash list

Then create the branch from that stash:

bash
git stash branch <branch-name> stash@{1}

Advanced Stash Usage

Stash with message

To add a meaningful message while stashing:

bash
git stash push -m "work in progress"

This makes it easier to identify stashes later, similar to writing clear messages in git commit message.

Stash specific files

To stash only selected files:

bash
git stash push file1 file2

This is useful when you don’t want to stash all changes in your working directory.

Stash before pull

If you have local changes and want to pull updates safely:

bash
git stash
git pull
git stash pop

This prevents conflicts while pulling changes, as explained in git pull command examples.


Common Errors and Fixes

no local changes to save

This error occurs when there are no modified or staged files.

Fix:

  • Run git status to verify changes
  • Ensure files are modified or staged

stash not applying

This can happen due to conflicts between stashed changes and current files.

Fix:

  • Resolve conflicts manually
  • Use:
bash
git stash apply

Understanding file differences using git diff examples can help resolve conflicts faster.

lost stash recovery

If a stash is accidentally dropped or lost, you can recover it using:

bash
git reflog

Then restore using:

bash
git reset --hard <commit-id>

👉 Learn more about recovery using git reflog tutorial.


Frequently Asked Questions

1. What is git stash?

Git stash is used to temporarily save local changes without committing them, allowing you to switch branches or pull updates without losing your work.

2. How do I stash local changes in Git?

You can stash local changes using git stash, which saves your changes and cleans your working directory.

3. How do I list stashed changes?

Use git stash list to view all saved stashes in your repository.

4. How do I apply a stash in Git?

You can apply a stash using git stash apply or remove it after applying using git stash pop.

5. Why do I get 'no local changes to save' in git stash?

This occurs when there are no modified or staged files in your working directory to stash.

Summary

Git stash is a powerful feature that allows you to temporarily save changes without committing them.

In this guide, you learned how to:

  • Stash and restore changes safely
  • Manage multiple stashes
  • Apply or remove stashes efficiently
  • Handle common errors and recover lost work

Using git stash effectively helps you switch tasks quickly, keep your working directory clean, and avoid unnecessary commits.


Official Documentation

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.