Git Stash Workflow
What you'll build
A practical workflow that uses stash to context-switch between branches without committing half-done work.
Goals
- Save uncommitted changes with git stash
- List, apply, and drop stashes
- Stash specific files with --keep-index
- Pop a stash and resolve conflicts
Prerequisites
- Basic Git commits and branches
- Working directory vs staging area
- Command line comfort
Steps
- 1
Create uncommitted work
On a feature branch, make some changes to a file but don't commit them. This simulates being mid-task when you need to switch context.
Codegit checkout -b feature/work echo 'half-done change' >> app.js # Now you get a bug report on main and must switch immediatelyExpected result
app.js has uncommitted changes; git status shows 'modified: app.js'.
- 2
Stash the changes
Run git stash to save your changes and restore the working directory to HEAD. Your work is saved but not committed, and the branch is clean.
Codegit stash git status # working tree cleanExpected result
Working directory is clean; git stash list shows one entry.
- 3
Switch context and fix the bug
Now that your branch is clean, you can switch to main, fix the bug, commit, and switch back. Without stash, this switch would fail or carry your changes over.
Codegit checkout main echo 'bug fix' >> bug.js && git add . && git commit -m 'Fix urgent bug' git checkout feature/workExpected result
You fixed the bug on main and returned to your feature branch.
- 4
Pop the stash to resume work
Run git stash pop to restore your saved changes. If there are conflicts (because main changed the same lines), resolve them before continuing.
Codegit stash pop # If conflict: # edit the conflicted file # git add <file> # git stash drop (the pop already removed it from the list on success)Expected result
Your half-done changes are back in the working directory.
- 5
Manage multiple stashes
Stash a second set of changes, then use git stash list to see both. Apply a specific stash with git stash apply stash@{1} and drop it with git stash drop stash@{1}.
Codeecho 'more changes' >> other.js && git stash git stash list # stash@{0}: WIP on feature/work: more changes # stash@{1}: WIP on feature/work: half-done change git stash apply stash@{1} # restore the first without removing it git stash drop stash@{1} # now remove it from the listExpected result
You can selectively apply and remove stashes from the list.
Try it yourself
# Run the commands above in a test repositorygit stash && git checkout main && git checkout - && git stash popWhat you learned
You mastered the stash workflow — the standard way to context-switch in Git without committing half-done work or losing changes.
Related Resources
Related Lessons
- Branching
Stash enables clean branch switching.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.