Understanding git stash
git stash
is a powerful Git command used to temporarily store changes you’ve made to your working directory, allowing you to switch contexts and later reapply them.
Stashing Changes
Use git stash
or git stash save "a descriptive message"
to stash your changes with an optional message. This is useful when your changes are not ready to be committed.
Listing Stashes
Run git stash list
to see the stashes you’ve stored. This command shows a list of stashed changes along with their identifiers.
Applying a Stash
To reapply stashed changes, use git stash apply <stash@{n}>
, where <stash@{n}>
is the identifier of the stash. If you don’t specify a stash, Git applies the most recent one.
Removing a Stash
After applying a stash, it’s still stored in the stash list. Remove it with git stash drop <stash@{n}>
. Alternatively, use git stash pop
to apply and immediately drop the most recent stash.
Handling Stash Conflicts
When applying a stash, merge conflicts might occur. Git will notify you of these conflicts, which you’ll need to resolve manually.
Technical Overview:
git stash
is ideal for quickly switching contexts without committing incomplete work.- Stashed changes are local to your Git repository and are not included in a
git push
. - The stash operates as a stack, where the last stash created is the first to be applied or dropped.
- Stashing is a temporary solution and should not replace proper commits.
This command is particularly useful for managing workflow disruptions without committing half-done work.