Table of Contents
When working on a project, it’s common to switch between tasks before you’re ready to commit your changes. In such cases, git stash is a lifesaver. The git stash command allows you to temporarily save your changes without committing them, making it easier to switch branches or work on different tasks. In this article, we will explore how to use git stash to manage your uncommitted changes efficiently.
What is git stash?
Git stash is a command that saves your uncommitted changes in a temporary storage area, known as the stash, without adding them to the commit history. This enables you to switch branches, pull in changes, or work on other tasks without losing your work in progress. Once you’re ready to resume, you can apply the stashed changes back to your working directory.

Basic usage of git stash
The most basic use of git stash is to save your current uncommitted changes so that you can work on something else. When you run the git stash command, Git saves the changes and resets your working directory to match the latest commit.
Example command
git stashAfter running this command, Git stashes your changes and clears your working directory. You can now switch branches, make other changes, or pull in updates without affecting your stashed changes.
Viewing stashed changes
Each time you run git stash, Git saves the changes in a stack-like structure. You can view all stashed changes using the git stash list command.
Example command
git stash listSample output:
stash@{0}: WIP on main: abc1234 Add feature X
stash@{1}: WIP on main: def5678 Fix bug YThe output shows a list of all stashes, with the most recent stash appearing at the top. Each stash is identified by an index (e.g., stash@{0}) and a message describing the stash.
Applying stashed changes
To retrieve and apply the most recent stashed changes to your working directory, use the git stash apply command.
Example command
git stash applyThis command applies the latest stash (stash@{0}) to your working directory. If you want to apply a specific stash from the list, use its index:
Example command
git stash apply stash@{1}After applying the stashed changes, they remain in the stash list. If you want to remove the stash after applying it, use the git stash pop command instead.
Example command
git stash popThis command applies the latest stash and removes it from the stash list.
Creating a named stash
By default, git stash saves a generic message like “WIP on branch-name.” To make it easier to identify a stash, you can provide a custom message when creating the stash.
Example command
git stash push -m "Save changes for feature Y"This command stashes your changes with the message “Save changes for feature Y,” making it easier to identify in the stash list.
Stashing only specific files
In some cases, you may want to stash only specific files instead of all uncommitted changes. You can do this by specifying the files you want to stash.
Example command
git stash push index.htmlThis command stashes changes to index.html while leaving other changes untouched in your working directory.
Removing stashed changes
If you no longer need the stashed changes, you can remove them using the git stash drop command.
Example command
git stash drop stash@{0}This command removes the specified stash (stash@{0}) from the stash list. To clear all stashes at once, use the git stash clear command.
Example command
git stash clearApplying stashed changes to a different branch
You can apply stashed changes to a different branch than the one you stashed them on. First, switch to the target branch, and then use git stash apply to apply the stash.
Example workflow
- Switch to the target branch:
git checkout target-branch- Apply the stash:
git stash apply stash@{0}This workflow allows you to transfer changes between branches without committing them.
Best practices for using git stash
While git stash is a convenient tool, it’s important to use it effectively to avoid confusion and ensure a smooth workflow.
1. Use meaningful stash messages
When creating a stash, provide a meaningful message to describe the changes. This makes it easier to identify and retrieve the stash later.
2. Clean up unused stashes
Stashes can accumulate over time, leading to a cluttered stash list. Regularly review and remove unused stashes using git stash drop or git stash clear.
3. Be cautious with stashes across branches
When applying stashes to a different branch, ensure that the changes are compatible with the target branch to avoid conflicts.
Example workflow using git stash
Let’s go through a typical workflow using git stash to temporarily save changes while switching tasks:
- Make changes to a file:
echo "New changes" >> index.html- Stash the changes:
git stash push -m "Save changes to index.html"- Switch to another branch:
git checkout another-branch- Work on the other branch and switch back:
git checkout main- Apply the stashed changes:
git stash applyThis workflow allows you to switch between tasks without losing your work in progress.
Conclusion
Git stash is a versatile and powerful command that allows you to temporarily save changes and switch between tasks without committing your work. By using git stash, you can maintain a clean working directory and efficiently manage changes in your project. Whether you’re switching branches, pulling updates, or pausing your work, git stash provides a flexible solution for managing uncommitted changes in your Git workflow.
By following best practices and understanding the various options available with git stash, you can streamline your development process and avoid losing important work in progress.