Back to Git & Dev Tools
2026-01-117 min read

Stashing and Cleaning (Git & Dev Tools)

Learn Stashing and Cleaning (Git & Dev Tools) step by step with clear examples and exercises.

Title: Git Stashing and Cleaning: A full guide for Developers

Why This Matters

As a developer, you often find yourself working on multiple tasks simultaneously, each requiring different changes to your codebase. However, sometimes you might need to switch to another task urgently without committing the current changes, which can be frustrating and time-consuming to manage. Git's stashing feature comes to the rescue by helping you save your uncommitted changes temporarily, allowing you to work on something else without losing your progress. In this lesson, we will explore how to use Git stashing and cleaning effectively to streamline your development process.

Prerequisites

To follow along with this lesson, you should have a basic understanding of:

  1. Git fundamentals, such as creating repositories, committing changes, and branching
  2. The command line interface (CLI) or a Git GUI like GitKraken or SourceTree
  3. Familiarity with common Git commands like git add, git commit, git checkout, and git status
  4. Understanding the concept of a Git branch and how to create, switch, and delete branches

Core Concept

What is Git Stashing?

Git stashing temporarily saves your local uncommitted changes, allowing you to switch to another task without losing your current work. When you stash your changes, Git creates a new commit that stores the changes in a stack. You can then apply the saved changes back to your working directory when needed.

How to Stash Changes

To stash your changes, run the following command in your terminal:

git stash save "stash message"

Replace "stash message" with a short description of your changes. This command creates a new stash entry and saves your uncommitted changes. If you don't provide a message, Git will automatically generate one based on the current branch name and the number of stashes.

Listing Stashed Changes

To view your stashed changes, run:

git stash list

This command displays a list of all your stashed changes along with their unique identifiers (stash@{...}).

Applying a Stashed Change

To apply a stashed change to your working directory, use the following command:

git stash apply "stash message"

Replace "stash message" with the message you provided when creating the stash. If you don't specify a message, Git will apply the most recent stash.

Deleting a Stashed Change

If you no longer need a stashed change and want to delete it, run:

git stash drop "stash message"

Again, replace "stash message" with the message you provided when creating the stash. This command removes the specified stash from the stack.

Clearing All Stashed Changes

To remove all stashed changes and discard them permanently, run:

git stash clear

This command deletes all stashes in the stack. Be cautious when using this command, as it will permanently delete your saved changes.

Stashing Changes Interactively

In some cases, you might want to customize which changes are stashed by interactively selecting specific files or hunks of code. To do this, run:

git stash -i "stash message"

This command opens an editor with a list of your uncommitted changes. You can then select the changes you want to stash and save the file to create the stash.

Stashing Changes on a Specific Branch

By default, Git stashes changes from the current branch. However, if you're working on a specific branch and need to stash only changes related to that branch, you can use the --index option:

git stash save --index "stash message"

This command will only stash changes that have been added to the index (i.e., staged changes) for the current branch.

Worked Example

In this example, we will demonstrate how to use Git stashing in practice:

  1. Create a new Git repository and navigate to it:
mkdir my-repo && cd my-repo
git init
touch file1.txt file2.txt
  1. Make some changes to both files:
echo "Hello World" > file1.txt
echo "This is a test" >> file2.txt
  1. Stage and commit the changes for the main branch:
git add .
git commit -m "Initial commit on main"
  1. Now, let's say you want to create a new feature branch (feature/new-feature) and make some changes there without committing them yet. Switch to the new branch:
git checkout -b feature/new-feature
  1. Make some changes on the feature/new-feature branch:
echo "New changes" > file1.txt
  1. Now, let's say you need to switch back to the main branch and want to save your current changes temporarily. Stash them using:
git stash save "Save feature/new-feature changes" --index
  1. Switch back to the main branch:
git checkout main
  1. Apply the stashed changes from the feature/new-feature branch:
git stash apply "Save feature/new-feature changes"
  1. Verify that your original changes have been applied:
cat file1.txt
  1. Now, let's delete the stashed change since we no longer need it:
git stash drop "Save feature/new-feature changes"

Common Mistakes

1. Forgetting to Stash Changes Before Switching Branches or Tasks

If you forget to stash your changes before switching branches or tasks, you might lose your work if the new task requires changes that conflict with your uncommitted changes. To avoid this, always remember to stash your changes before switching contexts.

2. Not Providing a Stash Message

Although not mandatory, providing a descriptive message for your stash can help you easily identify and manage your saved changes later on.

3. Confusing Git Stash with Git Add -p

Git add -p is used to selectively stage hunks of code for committing, while Git stash saves your uncommitted changes temporarily. Be careful not to confuse these two commands and use them appropriately based on your needs.

4. Stashing Changes from Multiple Branches Simultaneously

When you stash changes, they are stored in the current branch's stash stack. If you switch branches without applying or deleting the stashed changes, you might end up with multiple stashes for the same uncommitted changes across different branches. To avoid this, always apply or delete stashed changes before switching branches.

5. Not Understanding the Difference Between Git Stash and Git Stash Apply

Git stash saves your changes as a new commit, while Git stash apply applies the changes from a specific stash to your working directory. Be sure to use these commands appropriately based on your needs.

Practice Questions

  1. What command do you use to save your current uncommitted changes as a stash?
  2. How can you view all your stashed changes?
  3. To apply a specific stashed change, what command do you use and what information should you provide?
  4. If you have multiple stashes and want to delete the most recent one, which command do you use?
  5. What happens when you run git stash clear?
  6. How can you stash changes from a specific branch?
  7. What is the difference between Git stash and Git stash apply?
  8. What happens if you forget to stash your changes before switching branches or tasks?
  9. Can you stash untracked files along with tracked files when stashing changes?
  10. How can you recover a lost stashed change using Git's reflog?

FAQ

Q: Can I stash changes from a specific file or hunk of code?

A: Yes, you can stash changes interactively by using the -i flag, which opens an editor with a list of your uncommitted changes. You can then select the changes you want to stash and save the file to create the stash.

Q: What happens if I have untracked files when I try to stash my changes?

A: If you have untracked files in your working directory, Git will automatically include them in the stash. However, if you want to exclude specific files from the stash, you can use the --include-untracked or --exclude-untracked options when creating the stash.

Q: Can I stash changes from a different branch?

A: Yes, you can stash changes from any branch in your repository. To do this, switch to the branch containing the changes you want to stash and then run the git stash save command as usual.

Q: What if I accidentally lose my stashed changes?

A: If you lose your stashed changes by accident (for example, by running git stash drop without specifying a message), you can recover them using Git's reflog. The reflog keeps track of all your Git operations, including stashes. To view the reflog for stashes, run:

git log --oneline --decorate -g

You can then find the hash of the lost stash and recover it using:

git stash apply <stash-hash>
Stashing and Cleaning (Git & Dev Tools) | Git & Dev Tools | XQA Learn