Never Lose Work with Git Stash
Git stash is a lifesaver when you need to quickly switch contexts without committing half-finished work. Perhaps you're deep in developing a feature when an urgent bug report comes in. You need to switch to the main branch immediately, but your working directory has uncommitted changes. This is where git stash
shines - it temporarily shelves your modifications so you can work on something else, then reapply them later.
The basic git stash
command takes your modified tracked files and staged changes and saves them on a stack of unfinished changes. Your working directory becomes clean, matching the HEAD commit. After fixing that urgent bug and committing the fix, you can return to your previous work with git stash pop
, which reapplies the most recent stash and removes it from the stack.
Git stash offers more sophisticated options for complex workflows. Use git stash save "descriptive message"
to name your stashes, making it easier to manage multiple saved states. git stash list
shows all stashes with their messages and timestamps. You can apply a specific stash with git stash apply stash@{2}
or create a branch from a stash with git stash branch feature-branch
. The --include-untracked
flag ensures new files are also stashed.
While stash is powerful, it's not meant for long-term storage. Stashes are local to your repository and can be accidentally deleted. For work you want to preserve longer-term, consider committing to a feature branch instead. But for those moments when you need to quickly context-switch without losing work in progress, git stash is an indispensable tool that every developer should master.