In Git, a stash temporarily stores your uncommitted changes so that you can switch contexts (like changing branches) without committing these changes. Managing stashes effectively ensures that you can work on multiple tasks simultaneously and retrieve or organize saved changes as needed.
To create a stash, use:
git stash
By default, Git stashes all modified and staged changes in tracked files. If you want to save a stash with a custom message, use:
git stash push -m "Message describing changes"
To view all saved stashes, use:
git stash list
Explanation: This command lists all stashes in a stack format, with each stash labeled as stash@{index}
and including your custom message or a default description.
stash@{0}: WIP on main: 1234abc Fix homepage layout
stash@{1}: WIP on main: 5678def Add API endpoint for users
Applying a stash reintroduces the saved changes to your working directory without removing it from the stash stack.
To apply a specific stash, use:
git stash apply stash@{0}
If you don’t specify a stash ID, Git will apply the latest stash.
To apply and immediately remove the stash from the stack, use pop
:
git stash pop stash@{0}
Explanation: pop
is useful when you don’t need to keep the stash after applying it, simplifying the stack.
While Git doesn’t support renaming stashes directly, you can work around this by applying the stash, creating a new stash with a new message, and dropping the original one.
Apply the stash you want to rename:
git stash apply stash@{0}
git stash push -m "New stash message"
brew install gnupg
This approach allows you to organize stashes with meaningful descriptions, especially useful in large projects where managing multiple stashes by name is crucial.
When stashes are no longer needed, you can delete them to keep your stack organized.
To delete a specific stash, use:
git stash drop stash@{1}
To remove all stashes at once, use:
git stash clear
Explanation: Clearing stashes is useful when old or unused stashes are cluttering your list, especially after you’ve applied or integrated the changes they contained.
You can inspect the contents of a stash to see what changes are saved without applying it.
To see a summary, use:
git stash show stash@{0}
For a detailed view with specific code changes:
git stash show -p stash@{0}
Explanation: -p
(patch mode) shows a diff of the changes saved in the stash, which is helpful for reviewing the exact modifications before deciding to apply or delete the stash.
By default, Git stashes only tracked files. You can include untracked and ignored files when creating a stash:
git stash push -u
git stash push -a
To stash specific parts of your changes, use interactive patch mode:
git stash push -p
Git will prompt you to select specific lines or changes to stash.
Managing stashes effectively is key to organizing temporary changes and maintaining a clean Git workflow. From basic stashing to advanced techniques, understanding how to create, apply, organize, and delete stashes equips you to work efficiently across multiple tasks. Happy coding !❤️