codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
📦

Git & GitHub

11 / 63 topics
9Branching and Merging10Resolving Merge Conflicts11Using git stash12Using git rebase
Tutorials/Git & GitHub/Using git stash
📦Git & GitHub

Using git stash

Updated 2026-04-20
4 min read

Using git stash

Introduction

Git is a powerful version control system that allows developers to manage changes to their codebase efficiently. One of the common challenges developers face is switching between different branches or tasks while having uncommitted changes in their working directory. This can lead to conflicts and lost work if not handled properly. Git stash provides a solution to this problem by temporarily saving your uncommitted changes, allowing you to switch contexts without losing any work.

In this tutorial, we will explore how to use git stash effectively, including creating stashes, applying them, and managing multiple stashes. We'll also cover best practices for using stashes in your workflow.

What is git stash?

Git stash allows you to temporarily save changes that are not yet ready to be committed. This can be particularly useful when you need to switch branches or work on a different task but don't want to commit the current changes.

Key Concepts

  • Stash: A temporary storage area for uncommitted changes.
  • Apply: Reapply stashed changes to your working directory.
  • Pop: Apply stashed changes and remove them from the stash list.
  • Drop: Remove a specific stash without applying it.
  • Clear: Remove all stashes.

Basic Usage

Creating a Stash

To create a stash, use the git stash command. This will save your uncommitted changes and revert your working directory to the last commit.

# Create a stash with default message
git stash

# Create a stash with a custom message
git stash -m "WIP: fixing bug in login form"

Listing Stashes

You can list all stashes using the git stash list command. This will show you a list of stashes along with their messages and unique identifiers.

# List all stashes
git stash list

Example output:

stash@{0}: WIP on feature-branch: abc1234 Add new feature
stash@{1}: WIP on main: def5678 Fix critical bug

Applying a Stash

To apply a stash, use the git stash apply command followed by the stash identifier. This will reapply the changes to your working directory but keep the stash in the list.

# Apply the most recent stash
git stash apply

# Apply a specific stash
git stash apply stash@{1}

Popping a Stash

To apply a stash and remove it from the list, use the git stash pop command. This is useful when you want to reapply changes and immediately discard the stash.

# Pop the most recent stash
git stash pop

# Pop a specific stash
git stash pop stash@{1}

Dropping a Stash

To remove a specific stash without applying it, use the git stash drop command followed by the stash identifier.

# Drop the most recent stash
git stash drop

# Drop a specific stash
git stash drop stash@{1}

Clearing All Stashes

To remove all stashes at once, use the git stash clear command.

# Clear all stashes
git stash clear

Advanced Usage

Stashing Untracked Files

By default, git stash does not include untracked files in the stash. To include them, use the -u or --include-untracked option.

# Stash including untracked files
git stash -u

Stashing Ignored Files

To include ignored files in the stash, use the -a or --all option.

# Stash including all files (untracked and ignored)
git stash -a

Stashing Specific Files

You can also stash specific files by specifying them after the git stash command.

# Stash only specific files
git stash app.js styles.css

Best Practices

  1. Use Descriptive Messages: Always use descriptive messages when creating stashes to make it easier to identify and manage them later.
  2. Limit Number of Stashes: Keep the number of stashes manageable by applying or dropping them regularly. Too many stashes can clutter your list and make it harder to find what you need.
  3. Regularly Apply Stashes: If a stash is no longer needed, apply or drop it as soon as possible to keep your working directory clean.
  4. Avoid Long-Lived Stashes: Try not to keep stashes for an extended period. This can lead to conflicts and make it harder to reapply changes.

Real-World Example

Let's walk through a real-world scenario where git stash is useful.

Scenario: Switching Branches with Uncommitted Changes

Imagine you are working on the feature-branch and have made some uncommitted changes. You need to switch to the main branch to fix a critical bug but don't want to commit your current work.

# Create a stash with a custom message
git stash -m "WIP: feature-branch changes"

# Switch to the main branch
git checkout main

# Fix the critical bug and commit the changes
git add .
git commit -m "Fix critical bug in login form"

After fixing the bug, you can switch back to feature-branch and reapply your stashed changes.

# Switch back to feature-branch
git checkout feature-branch

# Apply the most recent stash
git stash pop

# Continue working on your feature branch

Conclusion

Git stash is a powerful tool that helps you manage uncommitted changes efficiently. By understanding how to create, apply, and manage stashes, you can switch between branches or tasks without losing any work. Remember to use descriptive messages, keep the number of stashes manageable, and regularly apply or drop them to maintain a clean and organized workflow.

In this tutorial, we covered the basics of using git stash, including creating, listing, applying, popping, dropping, and clearing stashes. We also explored advanced usage such as stashing untracked and ignored files, and discussed best practices for effective use in your development workflow.


PreviousResolving Merge ConflictsNext Using git rebase

Recommended Gear

Resolving Merge ConflictsUsing git rebase