
Git is one of the most popular version control systems used by developers to manage source code, track changes, and collaborate on software projects. During development, programmers frequently need to switch between branches, fix urgent bugs, review code, or experiment with new features. However, unfinished changes in the working directory can sometimes make these tasks difficult. This is where Git stash commands become extremely useful.
Git stash commands allow developers to temporarily save their uncommitted changes without creating a permanent commit. These changes can be restored later when the developer is ready to continue working. Whether you are building a web application, developing a mobile app, or maintaining a large software project, understanding Git stash commands can improve your workflow and help you manage code more efficiently.
In this guide, we will explore 10 useful Git stash commands every developer should know. You will learn their syntax, practical examples, use cases, and important tips for managing temporary changes safely.
What Is Git Stash and Why Is It Important?
Git stash is a built-in Git feature that temporarily stores modifications from your working directory and index. It allows you to clean your current workspace without losing the changes you have made.
For example, imagine you are developing a new login page for a website. Suddenly, your team asks you to fix a critical issue on another branch. Your current changes are incomplete, so committing them may not be appropriate. Instead, you can use Git stash to save your work temporarily, switch branches, fix the issue, and later restore your original changes.
Git stash is important because it helps developers:
- Save unfinished code without creating unnecessary commits.
- Switch between branches more easily.
- Handle urgent bug fixes.
- Keep the working directory clean.
- Manage multiple development tasks.
- Reduce interruptions during coding.
Git stash is especially useful for developers who frequently work with multiple branches or collaborate with other team members.
10 Useful Git Stash Commands Every Developer Should Know
The following Git stash commands cover common situations developers encounter during everyday programming. Each command includes its purpose and an example.
1. git stash
The git stash command saves your current tracked changes and restores your working directory to a clean state.
Syntax:
git stash
Example:
git stash
Suppose you have modified several files while developing a feature. Before switching to another branch, you can execute this command to temporarily save your work.
Git stores the changes in the stash stack, allowing you to restore them later.
Use case: Temporarily save unfinished work before changing branches.
2. git stash push -m
The git stash push -m command saves changes with a descriptive message. This is useful when you have multiple stashes and want to identify them easily.
Syntax:
git stash push -m “message”
Example:
git stash push -m “Working on login page”
The message helps you understand what the stash contains without inspecting every saved change.
Developers working on multiple features can use meaningful messages such as:
git stash push -m “Navbar redesign”
git stash push -m “API integration changes”
Use case: Organize temporary changes with descriptive labels.
3. git stash list
The git stash list command displays all saved stashes. Git stores them in a stack, with the newest stash generally appearing at the top.
Syntax:
git stash list
Example output:
stash@{0}: On main: Navbar redesign
stash@{1}: On feature-login: Login page changes
stash@{2}: On main: API integration
Each stash has a reference such as stash@{0}. You can use these references to apply, inspect, or remove specific stashes.
Use case: Find previously saved changes.
4. git stash pop
The git stash pop command restores the most recent stash and removes it from the stash list if the application succeeds.
Syntax:
git stash pop
Example:
git stash pop
This command is useful when you want to continue working on your saved changes.
However, conflicts can occur if the current branch has modified the same files. In such cases, you may need to resolve the conflicts manually.
Use case: Restore saved changes and remove the stash entry after successful application.
5. git stash apply
The git stash apply command restores saved changes without removing the stash from the stash list.
Syntax:
git stash apply
Example:
git stash apply stash@{1}
This command allows you to reuse the same stash multiple times. It is useful when you need to apply saved changes to different branches or want to keep a backup of your temporary work.
Unlike git stash pop, the stash remains available after applying it.
Use case: Restore changes while keeping the saved stash for future use.
6. git stash drop
The git stash drop deletes a chosen entry from Git’s stash list. .
Syntax:
git stash drop stash@{0}
Example:
git stash drop stash@{1}
This command is useful when you no longer need a particular saved change.
Be careful when using it because removing a stash can make its saved changes difficult to recover.
Use case: Delete unnecessary stash entries.
7. git stash clear
The git stash clear command removes all stash entries from the repository.
Syntax:
git stash clear
Example:
git stash clear
This command can help clean up an outdated stash list. However, it should be used carefully because it removes all saved stashes, not just one.
If you have important unfinished work, review your stash list before clearing it.
Use case: Remove all temporary saved changes when they are no longer needed.
8. git stash -u
The git stash -u command saves changes from tracked files along with untracked files.
Syntax:
git stash -u
Example:
git stash -u
Normally, git stash saves changes to files Git already tracks. If you have created new files that are not yet tracked by Git, the -u option allows you to include them in the stash.
This can be particularly useful when creating new components, configuration files, or scripts.
Use case: Save both tracked modifications and untracked files.
9. git stash -a
The git stash -a command saves tracked changes, untracked files, and ignored files.
Syntax:
git stash -a
Example:
git stash -a
The -a option is more comprehensive than -u. It includes ignored files as well, such as files excluded through .gitignore.
This command can help when you need a completely clean working directory. However, be cautious because ignored files may include local configuration or generated files that you do not want to stash.
Use case: Temporarily save nearly all working directory changes, including ignored files.
10. git stash branch
The git stash branch command creates a new branch from the commit where a particular stash was originally created and applies the saved changes to that branch.
Syntax:
git stash branch branch-name stash@{0}
Example:
git stash branch feature-login stash@{0}
This command is useful when the saved changes were created on a different branch and may conflict with your current branch.
Git attempts to apply the stash in the original context, which can make recovering unfinished work easier. When the stash is successfully applied, Git normally removes that stash entry.
Use case: Recover saved work in a separate branch.
Practical Git Stash Workflow for Developers
Understanding individual commands is useful, but knowing how to combine them is even more valuable.
Consider a situation where you are working on a website’s dashboard and need to switch to another branch to fix a bug.
A practical workflow could look like this:
Step 1: Check your current changes
git status
This command displays modified and untracked files.
Step 2: Save your changes
git stash push -u -m “Dashboard development”
This saves tracked changes and untracked files with a descriptive message.
Step 3: Switch to another branch
git switch bug-fix
Now you can work on the urgent issue.
Step 4: Complete the bug fix
git add .
git commit -m “Fix dashboard loading issue”
Step 5: Return to your previous branch
git switch main
Replace main with the actual branch where your original work was performed.
Step 6: Restore your saved changes
git stash pop
Your dashboard development work can now continue.
This workflow allows developers to handle interruptions without creating unnecessary temporary commits.
Common Git Stash Mistakes and Best Practices
Although Git stash is convenient, developers should use it carefully to avoid losing important work.
Common Mistakes
Using git stash clear without checking the list: This removes all stash entries and may delete important temporary work.
Confusing pop with apply: The pop command attempts to remove the stash after applying it, while apply keeps it in the list.
Ignoring untracked files: Newly created files may not be saved unless you use -u or -a.
Applying a stash to the wrong branch: Changes may conflict with the current branch or produce unexpected results.
Using vague stash messages: Messages such as “changes” or “work” make it difficult to identify saved modifications.
Best Practices
- Always review your working directory with git status.
- Use descriptive stash messages.
- Check your stash list before deleting entries.
- Prefer git stash apply when you want to preserve a stash.
- Use git stash pop when you no longer need the stash entry.
- Avoid clearing all stashes unless you are certain they are unnecessary.
- Commit

[…] American College of Sports Medicine (ACSM) […]