Git Stash
June 28, 2024Git is an incredibly powerful tool for version control, but it’s not just about tracking changes. It’s also about managing your workflow efficiently. One of the features that makes Git so versatile is git stash
. In this guide, we’ll explore what git stash
is, how it works, and some common scenarios where it can be particularly useful.
What is Git Stash?
git stash
is a command in Git that allows you to temporarily save changes in your working directory that you don’t want to commit immediately. This can be incredibly useful when you need to switch contexts, such as checking out another branch or pulling in the latest changes from the remote repository, without having to commit your incomplete or experimental changes.
When you run git stash
, Git saves your current working directory state (including modified tracked files and staged changes) in a special stack of stashes. This stack behaves similarly to a stack data structure in programming, following a Last In, First Out (LIFO) order.
How Git Stash Works
Here’s a step-by-step explanation of how git stash
works:
-
Saving Changes: When you execute
git stash
, Git takes the changes in your working directory and saves them on the stash stack. The working directory is then reset to match the HEAD commit, effectively giving you a clean slate to work from. -
Applying Changes: Later, when you’re ready to resume your work, you can reapply the stashed changes using
git stash apply
. This reapplies the changes from the stash to your working directory but keeps the stash in the stack for future use. -
Popping Changes: If you want to apply the changes and remove them from the stash stack, you can use
git stash pop
, which is essentially a combination ofgit stash apply
followed bygit stash drop
. -
Listing Stashes: You can view all stashed changes using
git stash list
. This will display a list of stashes, each with a unique identifier and a brief description of what was stashed. -
Dropping Stashes: To remove a specific stash from the stack, you can use
git stash drop
, followed by the stash reference.
Basic Git Stash Commands
Here’s a quick overview of the most common git stash
commands:
-
Stash Changes:
git stash
-
Stash Changes with a Message:
git stash save "message"
-
List Stashes:
git stash list
-
Apply Stash:
git stash apply
-
Pop Stash:
git stash pop
-
Drop Stash:
git stash drop
-
Clear All Stashes:
git stash clear
Example Use Cases
Let’s explore some practical scenarios where git stash
can be particularly useful:
1. Switching Branches
Imagine you’re working on a new feature in a branch, but suddenly, you need to switch to the main
branch to fix a bug. You don’t want to commit incomplete changes, so you stash them:
git stash
git checkout main
# Fix the bug and commit changes
git checkout feature-branch
git stash pop
2. Keeping the Working Directory Clean
You might want to pull the latest changes from a remote branch, but you have local changes that you don’t want to commit just yet. Use git stash
to save your changes:
git stash
git pull origin main
git stash pop
3. Experimenting with Code
When experimenting with code, you might want to test some ideas without committing. Stashing allows you to revert to the previous state quickly:
git stash
# Experiment with code
# Decide to discard changes
git stash drop
4. Applying a Stash to Another Branch
You might have stashed changes in one branch but decide to apply them to another:
git stash
git checkout another-branch
git stash apply
5. Stashing Untracked Files
By default, git stash
ignores untracked files. However, you can include them by using:
git stash -u
Or, for a more complete stash, including ignored files:
git stash -a
6. Creating a Branch from a Stash
If you want to create a new branch from a stash, you can do so with:
git stash branch <branch-name>
This command creates a new branch with the stashed changes applied.
Advanced Stashing Techniques
Stashing Specific Files
Sometimes, you may want to stash changes from specific files instead of stashing everything. You can do this by specifying the paths to the files you want to stash:
git stash push <file1> <file2>
Stashing with Index
The --keep-index
option allows you to stash only the changes in your working directory, preserving the staged changes:
git stash --keep-index
This is useful when you want to test your staged changes without the interference of other modifications.
Stashing Partially
If you want to stash only a portion of the changes in a file, you can use the git stash save -p
command. This will launch an interactive prompt where you can choose which changes to stash:
git stash save -p
Stashing Unmerged Paths
During a merge conflict, you might want to stash your working directory’s changes without resolving the conflicts immediately. You can do so with:
git stash save --merge
This command stashes both the changes and the conflict markers, allowing you to revisit the merge later.
Stashing with a Custom Message
Adding a custom message to your stash can make it easier to remember the context of the stashed changes:
git stash save "Work in progress on feature X"
You can then list your stashes with git stash list
to see your stashed changes along with your custom messages:
stash@{0}: On feature-branch: Work in progress on feature X
stash@{1}: On feature-branch: Initial draft
stash@{2}: On main: Hotfix changes