From saving your changes to applying and managing stashes, mastering Git stash helps you maintain a clean and organized codebase while multitasking in complex projects. Learn how to use this powerful feature with practical examples and best practices.
What is Git Stash?
Git stash temporarily saves changes in your working directory and staging area, allowing you to switch branches or perform other tasks without losing progress. The stashed changes are stored on a stack, which you can access and apply later.
Basic Git Stash Commands
Stashing Changes
To stash your current changes, use:
git stash
This saves all modified and staged files and resets your working directory to match the last commit. Add a message to describe the stash:
git stash push -m "WIP: Add login feature"
Listing Stashes
To view all stashes in the stack, use:
git stash list
Each stash is labeled with an identifier, such as stash@{0}
.
Applying a Stash
To apply the latest stash without removing it from the stack:
git stash apply
To apply a specific stash, specify its identifier:
git stash apply stash@{2}
Dropping a Stash
Remove a specific stash from the stack:
git stash drop stash@{0}
Popping a Stash
To apply the latest stash and remove it from the stack:
git stash pop
Clearing All Stashes
To remove all stashes from the stack:
git stash clear
Example: Using Git Stash in a .NET Project
Suppose you’re working on a .NET project, and you’ve made changes to Program.cs
but need to switch branches. The file contains the following code:
// Program.cs using System; namespace GitStashExample { class Program { static void Main(string[] args) { Console.WriteLine("Adding a new feature..."); } } }
To save your changes temporarily, stash them:
git stash push -m "WIP: New feature in Program.cs"
Switch to another branch, then return and reapply the stash:
git stash list git stash apply stash@{0}
Make further edits, then commit your changes:
git add Program.cs git commit -m "Complete new feature in Program.cs"
Advanced Git Stash Techniques
- Stashing Specific Files: Save changes in specific files:
git stash push
- Including Untracked Files: By default, Git stash ignores untracked files. Include them with:
git stash push -u
- Stashing Ignored Files: Save ignored files with:
git stash push -a
Best Practices for Using Git Stash
- Use Descriptive Messages: Always add messages to identify stashes easily.
- Keep Stashes Temporary: Avoid accumulating stashes; apply or drop them regularly.
- Be Cautious with Untracked Files: Ensure you don’t lose untracked files when stashing.
Common Pitfalls and Solutions
- Conflict on Applying Stash: If conflicts arise, resolve them manually and continue.
- Accidentally Dropped Stash: Unfortunately, dropped stashes cannot be recovered. Use
git stash apply
cautiously.
Conclusion
Git stash is a lifesaver for developers juggling multiple tasks. By learning how to save, apply, and manage stashes effectively, you can maintain a clean workspace and streamline your development process. Use Git stash to stay organized and flexible in your workflow.