Mastering Git: Stashing Changes with 'git stash' and 'git stash pop'
Course Title: Version Control Systems: Mastering Git
Section Title: Advanced Git Techniques
Topic: Stashing changes: git stash
and git stash pop
Overview
In this topic, you'll learn about the git stash
command and how to use it to temporarily store changes, allowing you to switch between branches or pull changes without losing your work-in-progress. We'll also cover git stash pop
, which helps you reapply the stashed changes to your working directory.
What is git stash
?
git stash
is a command that saves your changes to a special stack, called the stash, without creating a commit. This is useful when you're working on a feature and need to switch to another branch or pull changes, but you're not ready to commit your work.
Why Use git stash
?
Using git stash
has several benefits:
- Avoid conflicts: Stashing your changes allows you to switch branches or pull changes without conflicts.
- Keep your workspace clean: You can stash your changes and clean your working directory, making it easier to work on other tasks.
- Save your progress: Stashing your changes saves your work-in-progress, so you can come back to it later.
Basic Usage of git stash
Here's an example of how to use git stash
:
- Make some changes to your files.
- Run
git add
to stage the changes (optional). - Run
git stash
to stash the changes.
For example:
$ git status
On branch feature/new-feature
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
$ git stash
Saved working directory and index state WIP on feature/new-feature: 4d59b35 Update README
HEAD is now at 4d59b35 Update README
Listing and Applying Stashes
To list all your stashes, use git stash list
:
$ git stash list
stash@{0}: WIP on feature/new-feature: 4d59b35 Update README
To apply a stash, use git stash apply
:
$ git stash apply stash@{0}
On branch feature/new-feature
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
git stash pop
git stash pop
is similar to git stash apply
, but it also removes the stash from the list.
Here's an example:
$ git stash list
stash@{0}: WIP on feature/new-feature: 4d59b35 Update README
$ git stash pop
On branch feature/new-feature
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
$ git stash list
# Empty list
Tips and Best Practices
- Use
git stash
when you need to temporarily save your changes. - Use
git stash pop
when you want to reapply the stashed changes and remove the stash from the list. - Consider using
git stash
with a descriptive message, likegit stash save "feature/new-feature: WIP"
. - Be mindful of the stash list, as it can grow large if you're not careful. You can use
git stash drop
to remove stashes.
Example Use Case
Suppose you're working on a feature branch and you've made some changes, but you need to switch to another branch to fix a bug. You can stash your changes, switch to the other branch, and then reapply the stashed changes when you're done.
Here's an example:
# On feature branch
$ git stash save "feature/new-feature: WIP"
# Switch to another branch
$ git checkout fix/bug
# Fix the bug and commit the changes
# Switch back to feature branch
$ git checkout feature/new-feature
# Reapply the stashed changes
$ git stash pop
Conclusion
In this topic, you've learned how to use git stash
and git stash pop
to temporarily store and reapply changes. This is a powerful tool that can help you manage your workflow and avoid conflicts.
What's Next?
In the next topic, we'll cover using tags for releases. We'll explore the different types of tags and how to create and manage them.
Additional Resources
Do you have any questions about git stash
and git stash pop
? Share your thoughts and ask for help in the comments below.
Images

Comments