Git Stash Changes: A Complete Guide
What is Git Stash?
In Git, stashing allows you to temporarily save your uncommitted changes (both staged and unstaged) in a "stash," so you can work on something else without losing your progress. This is particularly useful when you need to switch branches but don’t want to commit unfinished work yet.
Think of stashing as a way to "shelve" your current work, allowing you to apply it later when you’re ready.
Why Use Git Stash?
Switching Branches Without Committing: If you’re working on a feature or bugfix and need to quickly switch to another branch, stashing saves your work so you can return to it later.
Temporary Storage: If you’re not ready to commit your changes but want to clear your working directory to test something or pull the latest changes from the repository, stashing can temporarily save your progress.
Experimental Changes: Sometimes, you want to test an idea but don’t want to commit it yet. Stashing allows you to save the experiment and return to your clean state.
Basic Git Stash Commands
Git provides a few commands to stash your changes, apply them, and manage the stashed changes.
Stash Changes
Stash Uncommitted Changes: To stash both staged and unstaged changes, use the following command:
This will save the current state of your working directory and leave you with a clean working directory.
Stash Only Staged Changes: If you only want to stash the staged changes (not the ones you’ve made but haven't staged), use:
Stash Changes with a Message: It’s often helpful to add a message to your stash to remind yourself of what changes were saved. You can do this by adding a
-m
option followed by your message:
List Stashes
To see a list of all stashed changes, use:
This will display all the stashes, along with their names (e.g., stash@{0}
, stash@{1}
, etc.) and any messages you provided when creating the stash.
Apply Stashed Changes
Once you’re ready to apply the changes from your stash, use the git stash apply
command. By default, this will apply to the most recent stash:
If you want to apply a specific stash, use its identifier:
Git Stash Pop
The a git stash pop
command is similar to git stash apply
, but with one key difference: it removes the stash from the stash list after it has been applied. This is useful when you’re done with the stash and don’t want to keep it around.
Or, apply a specific stash and remove it from the list:
Stash Changes and Switch Branches
One of the most common use cases for stashing is when you need to switch branches without committing your work. Here’s how you can do that:
Stash Changes:
Switch Branches: You can now safely switch branches without worrying about uncommitted changes:
Apply Stash When Ready: After switching branches, you can apply the stash to bring your previous work back:
Stash Untracked or Ignored Files
By default, Git only stashes tracked files. However, if you want to stash untracked or ignored files as well, you can use the -u
or -a
options, respectively:
Stash Untracked Files:
This will stash both staged and unstaged changes, including untracked files.
Stash Ignored Files:
This will stash all files, including ignored ones.
Drop a Stash
If you no longer need a particular stash, you can remove it using the git stash drop
command:
This will remove the specific stash from the list.
Clear All Stashes
If you want to delete all stashed changes, use:
This will remove all stashes from the list, so be careful when using this command!
Apply a Stash to a Different Branch
You can stash changes on one branch and apply them to another branch if needed. Here’s how:
Stash the Changes on the Current Branch:
Switch to the Target Branch:
Apply the Stash to the New Branch:
This allows you to carry over changes between branches without committing them.
Stash Workflow Example
Let’s go through a common workflow to illustrate stashing in action:
You’re working on
featureA
and make some changes, but you need to quickly fix a bug onmaster
.Switch to the
master
branch to fix the bug:After fixing the bug, commit the changes on
master
:Now switch back to the
featureA
branch:Apply your stashed changes to continue working on
featureA
:Once you’re done with the feature, commit your changes:
Conclusion
Git stash is a powerful tool that allows you to temporarily save your work and return to it later, without the need for creating intermediate commits. It’s particularly useful when you need to switch branches or test something without losing your progress. By mastering stashing, you can improve your workflow and avoid interruptions in your development process.
This guide should provide a complete understanding of Git stashing for your website readers. Let me know if you need further clarification or additional examples!