When working on a Git project, there are situations where you may need to save uncommitted changes temporarily without committing them. Git’s stash feature allows you to "stash" these changes, temporarily saving your work and clearing the working directory. Later, you can reapply these stashed changes to continue where you left off.
The git stash
command allows users to temporarily save (or “stash”) uncommitted changes in their working directory, making it easy to switch contexts without losing ongoing work.
To stash all modified and staged changes, use:
git stash
This command takes the current state of your working directory and saves it in the stash list, while your working directory reverts to the latest commit state.
By default, Git assigns a default name to each stash, but you can give it a custom name for easier reference
git stash save "my-feature-work"
Explanation: save "my-feature-work"
stores your changes with the specified name, making it easier to identify later.
When you’re ready to bring back your stashed changes, Git provides two main commands: git stash apply
and git stash pop
.
git stash apply
git stash apply
This command reapplies the most recent stash to your working directory without removing it from the stash list.
git stash pop
git stash pop
This command applies the most recent stash and removes it from the stash list.
pop
is often used when you want to reapply changes and clear the stash since you no longer need to keep it stored.If you have multiple stashes, you can specify which one to apply by referencing its position in the stash list:
git stash apply stash@{1}
Explanation: stash@{1}
refers to the second stash in the list, allowing you to apply a specific stash rather than the most recent one.
You can list all stashes in the stash stack using:
git stash list
Explanation: This command shows all saved stashes, including their names or default descriptions, enabling you to identify which stash to apply.
Partial stashing allows you to stash specific files or even specific changes within files.
To stash changes in particular files only, use:
git stash push -p file1.txt file2.txt
Explanation: This command stashes only changes in file1.txt
and file2.txt
, leaving the rest of the working directory untouched.
-p
flag)The -p
or --patch
option lets you select specific changes to stash.
git stash push -p
Explanation: When you use -p
, Git will walk you through each change interactively, letting you choose which ones to stash.
If you only want to stash working directory changes but keep staged files in the index, use:
git stash --keep-index
Explanation: This command stashes only the unstaged changes, leaving staged changes intact in the index, making it useful when you’re selectively stashing.
If you have untracked files that you also want to stash, use:
git stash -u
-u
flag includes untracked files in the stash, storing all changes made to files not yet tracked by Git.Once you no longer need a stash, it’s a good idea to delete it to keep your stash list manageable.
Use:
git stash drop stash@{1}
To delete all stashes at once, use:
git stash clear
Explanation: This command removes all stashes, which can be helpful for keeping your project clean after you’ve applied or no longer need certain stashes.
Stashing is a powerful feature in Git that allows you to save your work temporarily and return to it when you’re ready. From basic usage to advanced techniques, understanding how to effectively use git stash will make handling multiple tasks and managing changes smoother. Happy coding !❤️