Git Notes
Master git stash to save work-in-progress changes without committing. Learn stash operations, named stashes, partial stashing, and workflow patterns.
Git stash temporarily shelves uncommitted changes so you can work on something else, then come back and re-apply them later. Think of it as a clipboard for your working directory.
Why Use Git Stash?
Common scenarios:
- Need to switch branches but have uncommitted work
- Want to pull latest changes without committing WIP code
- Need to quickly test something on a clean working directory
- Interrupted by urgent bug fix while working on a feature
Basic Stash Operations
Stash Changes
git stashSaved working directory and index state WIP on feature: a1b2c3d Add header component
Your working directory is now clean:
git statusOn branch feature nothing to commit, working tree clean
Stash with a Message
git stash push -m "WIP: user authentication form"Saved working directory and index state On feature: WIP: user authentication form
View Stashed Changes
git stash liststash@{0}: On feature: WIP: user authentication form
stash@{1}: WIP on main: b2c3d4e Fix navigation
stash@{2}: WIP on develop: c3d4e5f Update API routesApply Stash (Keep in Stash List)
# Apply most recent stash
git stash apply
# Apply a specific stash
git stash apply stash@{2}Pop Stash (Apply and Remove from List)
# Apply most recent stash and remove it
git stash pop
# Pop a specific stash
git stash pop stash@{1}On branch feature
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: src/auth.js
modified: src/login.css
Dropped stash@{0} (a1b2c3d4e5f6789012345678901234567890abcd)Drop a Stash
# Remove a specific stash
git stash drop stash@{1}
# Clear ALL stashes
git stash clearAdvanced Stash Options
Stash Including Untracked Files
# By default, stash only saves tracked files
# Include untracked files:
git stash push -u
# or
git stash push --include-untrackedStash Including Ignored Files
git stash push -a
# or
git stash push --allStash Specific Files (Partial Stash)
# Stash only specific files
git stash push -m "stash auth files" src/auth.js src/login.js
# Interactive partial stash (choose hunks)
git stash push -pdiff --git a/src/app.js b/src/app.js index a1b2c3d..e5f6g7h 100644 --- a/src/app.js +++ b/src/app.js @@ -1,5 +1,8 @@ +import auth from './auth'; + const app = express(); +app.use(auth.middleware); Stash this hunk [y,n,q,a,d,/,s,e,?]?
Keep Staged Changes
# Stash only unstaged changes (keep staged as-is)
git stash push --keep-indexViewing Stash Contents
Show Stash Diff
git stash showsrc/auth.js | 25 +++++++++++++++++++++++++ src/login.css | 8 ++++++++ 2 files changed, 33 insertions(+)
# Show full diff
git stash show -p
# or for a specific stash
git stash show -p stash@{2}diff --git a/src/auth.js b/src/auth.js
index a1b2c3d..e5f6g7h 100644
--- a/src/auth.js
+++ b/src/auth.js
@@ -0,0 +1,25 @@
+const jwt = require('jsonwebtoken');
+
+function authenticate(req, res, next) {
+ const token = req.headers.authorization;
+ // ... implementation
+}Stash and Branch
Create Branch from Stash
# Creates a new branch, checks it out, and applies the stash
git stash branch new-feature-branch stash@{0}Switched to a new branch 'new-feature-branch'
On branch new-feature-branch
Changes not staged for commit:
modified: src/auth.js
Dropped stash@{0} (abc1234...)This is useful when your stashed changes conflict with newer changes on the original branch.
Practical Workflows
Quick Context Switch
# Working on feature, urgent bug reported
git stash push -m "WIP: feature in progress"
git switch main
git switch -c hotfix/urgent-bug
# Fix the bug
git add . && git commit -m "Fix critical bug"
git switch main && git merge hotfix/urgent-bug
git push origin main
# Return to feature work
git switch feature-branch
git stash popPull Without Committing WIP
# You have uncommitted changes and need to pull
git stash
git pull origin main
git stash pop
# Resolve any conflicts if neededTest on Clean Working Directory
git stash
npm test # Run tests on clean state
git stash popHandling Stash Conflicts
If popping a stash causes conflicts:
git stash popAuto-merging src/app.js CONFLICT (content): Merge conflict in src/app.js The stash entry is kept in case you need it again.
The stash is NOT dropped when there are conflicts. Resolve and drop manually:
# Resolve conflicts in the file
git add src/app.js
# Drop the stash manually
git stash dropInterview Questions
- What is the difference between
git stash popandgit stash apply?
Both re-apply stashed changes to your working directory. pop removes the stash from the list after applying. apply keeps the stash in the list so you can apply it to multiple branches.
- How do you stash untracked files?
Use git stash push -u (or --include-untracked). By default, git stash only saves changes to tracked files. Use -a to also include ignored files.
- What happens if applying a stash causes conflicts?
Git marks the conflicts in the files (similar to merge conflicts). With pop, the stash is NOT dropped, so you can try again later. You must resolve conflicts manually and drop the stash yourself.
- How do you stash only specific files?
Use git stash push -m "message" file1 file2 to stash specific files, or git stash push -p for interactive selection of individual hunks.
- When should you use stash vs creating a WIP commit?
Use stash for quick temporary saves when switching context. Use WIP commits when the work-in-progress might last longer or when you want the safety of being on a branch with commit history.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Stash - Temporarily Save Uncommitted Changes.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, advanced, stash, git stash - temporarily save uncommitted changes
Related Git & GitHub Topics