Key Takeaways:
- Git Stash temporarily saves your uncommitted changes so you can work on something else without committing.
- Use Git Stash when switching branches, fixing urgent bugs, or pausing unfinished work.
- The git stash command saves tracked file changes, while git stash -u includes untracked files, and git stash -a includes ignored files.
- Use git stash pop to apply and remove the latest stash, or git stash apply to apply without removing it.
- Partial stashing is possible with git stash -p to save only selected changes.
- Git Stash is temporary and local, unlike Git Commit, which is permanent and shared in history.
- Stashes are not pushed to remote and may be lost if overused without tracking.
Table of Contents:
Once you have mastered adding, committing, and branching, the next tool that comes in handy during development is stash, or stashing. Suppose you are working on an important project and are on the main or master branch. You create a new branch, switch to it, and start working right away. You are halfway through the feature and not ready to commit yet. But let’s say a teammate needs your help in another branch. Since you haven’t committed yet, what happens to that half-done work when you switch back to the main branch?
In this article, we will learn what Git Stash is, how it works, and when to use it. We will also cover key commands, use cases, and tips to manage changes without committing.
What Is Git Stash in Git?
Let’s understand what stash does:
“Git Stash is used to temporarily save your changes or hide them in the background of Git so you can come back to them later without having to make unnecessary commits”.
There are times when you need to switch tasks or fix a bug, but you’re already working on a different branch and midway through, not ready to commit your changes. Git stash lets you save your uncommitted changes in a separate place so you can return later and restore your work.
The stash stores changes from both:
- The working directory (unstaged changes)
- The staging area (index) (staged changes)
Why and When to Use Git Stash
Here are some common use cases:
- Switch branches: You can save your work before changing branches.
- Handle other priorities: You can stash your work to deal with a higher-priority task, then restore it later.
- Keep your work-in-progress safe: You can avoid messy commits or losing your half-done changes.
Note: Stash saves your changes temporarily in a special stash stack maintained by Git, preserving both staged and unstaged changes, all without requiring a commit.
How Git Stash Works Internally
If you only want to learn how to use git stash, you can skip this part. But if you’re curious about how Git handles stash behind the scenes, this section explains it in a simple way.
1. What Happens Internally When You Run git stash
When you run git stash, Git saves your changes as commits in the background. It stores them in a special place called .git/refs/stash.
- The latest stash is saved as stash@{0}.
- Older stashes are saved as stash@{1}, stash@{2}, and so on.
You can view these with the stash list or use commands like git log refs/stash.
2. What Gets Saved When You Use git stash
- Depending on your changes, Git may create two or three commits:
- One commit for the files you changed but didn’t stage.
- One commit for the files you did stage.
An optional third commit for untracked or ignored files if you used --include-untracked or --all. These commits are grouped together and form a stash.
3. Git Stash Commit Structure Explained
The diagram below shows how Git handles different types of files when you stash:
a. git stash
- Saves tracked file changes only.
- Creates two commits: one for unstaged changes, one for staged changes.
b. git stash -u or --include-untracked
- Also saves untracked files.
- Creates a third commit to hold them.
c. git stash -a or --all
- Saves ignored and untracked files as well.
- Adds another commit for them.
4. How git stash pop Works and What It Does
When you run git stash pop:
- The commits are not deleted immediately but may be removed later by Git’s garbage collection.
- Git applies the saved changes back to your working directory and staged area.
- Then, it removes that stash from the stash list.
Stash Your Changes (git stash) With Example.
Git Stash is a super useful command that helps you save changes that you are not ready to commit. The command is git stash; you can also use git stash save.
You can stash changes and then come back to them later. This will take all your uncommitted changes, whether staged or unstaged, and stash them away.
So that reverting to those changes in your working directory remains easy when you want to.
Let’s understand Stash by using a git stash command example:
Let’s say you are working on some code, as shown below, and you’re not ready to commit yet. But a colleague on Teams pings you to check out what they’ve just merged into main. So, you try to switch to the main branch.
Say you are currently on the feature-one branch. You run git switch main, but you fail.
An error appears:
“Your local changes to the following files would be overwritten by checkout. Please commit or stash your changes before you switch.”
Real-Life Use Case: When to Use Git Stash
When changes are not ready to be committed but the developer needs to switch branches, stash becomes the immediate solution.
Suppose a developer runs git stash. Instantly, the file with uncommitted and unstaged changes appears clean in the working directory. Those changes are not deleted—they’re temporarily saved in Git’s stash stack.
Git provides a confirmation message:
“Saved working directory and index state WIP on feature-one…” along with a hash ID, indicating that the changes have been successfully stashed.
How to Switch Branches After Stashing Changes
Now, with the working directory clean, the developer can safely switch branches using git switch main
They’re free to view or work on any other task, like reviewing a teammate’s changes on the main branch, without risk of losing their work.
Restoring Stashed Changes Using git stash pop
Once done, the developer returns to their feature branch (feature-one) and wants to resume work. To restore the most recently stashed changes, they use git stash pop
This git stash pop example removes the most recent stash entry from the stack and applies it back to the working directory, reloading all saved changes in their exact state.
At least this is one option. This will remove the most recently stashed change or changes and reapply them to my working copy. So you can use git stash pop, just like that, to get your changes back.
Use git stash pop to remove the most recently stashed changes in your stash and reapply them to your working copy.
Let’s quickly run through some other details around stashing!
Stash Apply
There’s a command called git stash apply that allows you to take whatever changes are in the stash and have them applied without removing anything from the stash. This can be useful if you want to apply stashed changes to multiple branches.
How to Stash Untracked and Ignored Files
Let’s understand how to stash changes in git.
By default, Git doesn’t stash untracked or ignored files. You can include them like this:
Include Untracked Files:
git stash -u
# or
git stash --include-untracked
Include All (tracked + untracked + ignored):
git stash -a
# or
git stash --all
If you have untracked files (files that you have never checked into Git), they will not be included in the stash. Fortunately, you can use the git stash -u option to tell git to include those git stash untracked files. To get all tracked, untracked, and ignored files, you can simply do git stash –all.
Stashing Multiple Times
You can add multiple stashes to the stack of stashes. They will all be stashed in the order you added them. You can keep adding to the stash using git stash. Each time you do that, you get your own little checkpoint in the stash.
They are added. They’re stashed in the order that you added them. You can view all of those stashes using git stash list.
How to Apply a Specific Stash
Now you can take one of these numbers, these identifiers, and you can apply one of those specific stashes to whatever branch you’re on.
Git stash apply stash@{2}
This would apply to whatever is in or whatever changes are stored here in this specific stash.
Create Stash with a Message or Named Stash
You can also add a message to recall what you stashed in git, or to know what that stash contains.
For example: Stash with a message
git stash push -m "WIP: contactpage redesign"
Saved working directory and index state On main: WIP: contactpage redesign
This command lets you add a description to your stats to help you recall what you were working on.
Dropping & Clearing Stashes
You can also drop stashes. To delete a particular stash, you can use
git stash drop <stash-id>
e.g., git stash drop stash@{2}
However, to clear all stashes, run git stash clear
So, there are a lot of stashing commands here!
How It Works – Git Stash
Saves changes:
When you run git stash (or git stash push), Git takes your uncommitted changes (both staged and unstaged tracked files) and saves them in a “stash stack.”
Cleans working directory:
After saving the changes, git stash reverts your working directory to the state of the HEAD commit, making it clean and allowing you to switch branches or perform other operations without conflicts from your unfinished work.
Manages multiple stashes:
You can create multiple stashes, and they are stored in a stack-like structure. The most recent stash is at the top (referenced as stash@{0}), and older stashes are referenced by increasing numbers (e.g., stash@{1}, stash@{2}).
Git Stash vs Git Commit
Let’s understand stash vs commit:
Feature | Git Stash | Git Commit |
Purpose | Temporary save | Permanent record |
Affects history? | No | Yes |
Use Case | Incomplete or WIP changes | Ready for review or merge |
Visibility | Only local | Shared via pushes |
Note: Use stash when you’re not ready to commit.
Common Mistakes with Git Stash
Mistake | Fix |
Stashing untracked files by mistake | Use -u or --include-untracked |
Losing stash after pop | Use git fsck or avoid using pop |
Overusing stash instead of branching | Prefer branches for large context shifts |
Advanced Git Stash Techniques
1. Partial Stashes in Git
Sometimes, you don’t want to stash all your changes, just a few files or even specific lines. Git makes this possible through partial stashing.
To stash only selected changes, use the -p or --
patch option:
git stash -p
This command will go through each modified “hunk” (a section of changes) in your working directory and ask whether you want to stash it. You can interactively choose:
- y – stash this hunk
- n – skip this hunk
- q – quit
- a – stash all remaining hunks
- d – view the diff
- s – split the hunk into smaller parts
This allows you granular control over what gets stashed, making it ideal for large files with mixed changes. There is no explicit “abort” command to stop this, but hitting CTRL-C (SIGINT) will abort the stash process.
Pro Tip:
Use this when you’re experimenting but only want to keep part of your changes for later.
2. Creating a Branch from a Stash
Sometimes, when you try to apply or pop a stash, you may encounter merge conflicts, especially if your current branch has diverged significantly from the changes stored in the stash.
To avoid this, Git provides a cleaner alternative:
git stash branch <branch-name>
This command does three things at once:
- Creates a new branch from the commit where you ran git stash.
- Check out that new branch.
- Applies the stashed changes to it.
Example:
git stash branch fix-login-issue
This creates a new branch called fix-login-issue with the stashed changes already applied, allowing you to resolve issues or continue work without affecting your original branch.
Pro Tip:
This is the safest way to work with stashed changes when your main branch has moved ahead or when you’re unsure about conflicts.
3. Creating a Branch from a Stash
git switch <branch> -c <new> && git stash push -m "temp changes"
Pro Tip:
If you want to level up your Git skills further, explore tools like git rebase, git cherry-pick, and git bisect.
Conclusion
Honestly, it all comes down to this: in your daily use, you will use git stash a lot. Later on, when you would want those changes back, use git stash pop. That covers everything you need to use Git Stash effectively in your workflow. Now, if you want to learn more about Git and GitHub, there’s a Git and GitHub course on Intellipaat for it. Hope this blog helped you to understand git stash!
Git Stash FAQs-
1. How to View, Rename, or Delete Stashes?
To view, rename, or delete the stash, run the following:
– View stashes: git stash list
– View stash content: git stash show -p stash@{0}
– Rename a stash: No direct rename, but you can pop and re-stash with a new message.
– Delete a stash: git stash drop stash@{1}
2. How to Stash Changes in a Specific File or Directory?
This stashes only the specified file:
git stash push -m “save index.html” index.html
3. How to recover Lost Stashes?
If you accidentally ran git stash pop and didn’t apply it properly, then do the following to recover lost stashes:
git fsck --lost-found
Or check stash logs:
git log -g refs/stash
4. Can I stash across branches?
Yes. You can stash in one branch and apply in another.
5. Will stash push to remote?
No. Stashes are local only.
6. How long does a stash last?
Until you drop or clear it, stashes are stored indefinitely.