Git Stash is a powerful yet underutilized feature that allows you to temporarily save your changes without committing them. This is especially useful when you need to switch branches or work on something else without losing your current work. In this post, we’ll explore how to use Git Stash effectively and integrate it into your workflow.
1. What Is Git Stash?
Git Stash temporarily shelves (or “stashes”) changes that haven’t been committed yet.
This allows you to:
- Save uncommitted changes.
- Work on a different branch or task.
- Come back to the stashed changes later.
Think of it as a way to “pause” your current work.
2. How to Use Git Stash
A. Basic Git Stash Command
To stash your current changes:
git stash
This command stores your working directory and index (staged changes), so you can work in a clean state.
B. Applying a Stash
When you’re ready to return to your stashed changes, use:
git stash apply
This applies the latest stashed changes to your current working directory without removing the stash from history.
C. Listing Stashes
You can view all your stashes with:
git stash list
This command shows a list of all stashed changes along with their stash ID.
D. Applying a Specific Stash
If you have multiple stashes, you can apply a specific one by referencing its stash ID:
git stash apply stash@{2}
E. Removing a Stash
After applying a stash, you might want to remove it to keep your stash list clean:
git stash drop
To remove a specific stash, use:
git stash drop stash@{2}
F. Stashing Only Staged Changes
If you only want to stash the changes that you’ve staged, run:
git stash --staged
G. Stashing Untracked Files
By default, Git Stash ignores untracked files. To include them, use:
git stash -u
3. Using Git Stash in Different Scenarios
A. Switching Branches Safely
You’re working on a new feature but suddenly need to fix a bug on another branch. Git Stash can temporarily save your progress so you can switch branches:
git stash
git checkout bugfix-branch
After fixing the bug, you can return to your feature branch and apply the stash:
git checkout feature-branch
git stash apply
B. Saving Work in Progress Without Committing
There are times when your work isn’t in a state that’s ready to be committed, but you need to switch tasks. Use Git Stash to shelve unfinished changes without committing incomplete code.
C. Keeping Your Workspace Clean
If you need to experiment with changes or temporarily adjust your code, stash your current work first. This ensures that your workspace stays clean, and you can revert back at any time:
git stash
4. Managing and Inspecting Stashes
A. Inspecting a Stash
To view the contents of a stash before applying it, use:
git stash show -p stash@{0}
This will display a detailed diff of the stashed changes.
B. Popping a Stash
Instead of applying a stash and then manually removing it, you can “pop” it. This applies the stash and removes it from the stash list:
git stash pop
C. Creating a Named Stash
For better organization, you can name your stashes, especially when working with multiple ones:
git stash save "WIP: refactor login logic"
This allows you to identify and manage stashes easily.
5. Conclusion
Git Stash is an essential tool for developers who need to multitask or manage unfinished work without losing progress. By mastering stashing techniques, you can ensure a cleaner, more organized workflow.