What if you could save just the changes you want and switch tasks instantly without fear of losing work?
Why Stashing specific files in Git? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are working on a project and have made changes to many files. Suddenly, you need to switch to a different task, but only want to save some of your changes temporarily without committing them.
Manually copying files or undoing changes to save only some files is slow and risky. You might lose work or mix up changes, causing confusion and errors.
Git's ability to stash specific files lets you save just the changes you want, keeping your work safe and your workspace clean. You can easily switch tasks and come back later without losing progress.
cp file1 file1_backup
rm file1
# switch tasks
cp file1_backup file1git stash push file1
# switch tasks
git stash popThis lets you quickly pause part of your work and switch focus without losing or mixing changes.
A developer is fixing a bug in one file but also started a new feature in another. They stash only the feature file changes, fix the bug, then restore the feature work seamlessly.
Manual saving of partial changes is slow and risky.
Stashing specific files keeps work safe and organized.
Switch tasks easily without losing progress.
Practice
git stash push -m "save changes" file.txt do?Solution
Step 1: Understand the
This command saves changes in the working directory to a stash instead of committing.git stash pushcommandStep 2: Recognize the effect of specifying a file
By addingfile.txt, only changes in that file are saved to the stash, not all files.Final Answer:
It saves changes only fromfile.txtto a new stash with a message. -> Option AQuick Check:
Stash specific file = It saves changes only fromfile.txtto a new stash with a message. [OK]
git stash push with file names to stash specific files [OK]- Thinking it stashes all files without specifying
- Confusing stash with commit
- Assuming it deletes files
-index.html and style.css files?Solution
Step 1: Recall the syntax for stashing specific files
The correct syntax usesgit stash push -- <files>to specify files.Step 2: Identify the correct option
git stash push -- -index.html style.css uses--before file names, which is required to separate options from file paths.Final Answer:
git stash push -- -index.html style.css -> Option AQuick Check:
Use--before files to stash specific files [OK]
-- before file names in stash command [OK]- Omitting
--before file names - Using unsupported flags like
-f - Adding extra words like 'files'
-file1.txt modified, file2.txt modified, file3.txt unchanged.What will be the output of
git stash push -- -file1.txt followed by git stash list?Solution
Step 1: Understand what
This command saves only changes fromgit stash push -- -file1.txtdoes-file1.txtto a new stash.Step 2: Check the stash list output
After stashing,git stash listshows the new stash entry with only-file1.txtchanges saved.Final Answer:
Shows a stash with only-file1.txtchanges saved. -> Option CQuick Check:
Stash specific file = stash list shows that file only [OK]
- Assuming all modified files are stashed
- Expecting an error when multiple files are modified
- Confusing stash list output with file contents
git stash push -file1.txt but get an error: error: unknown option '-file1.txt'. What is the likely cause?Solution
Step 1: Analyze the error message
The error saysunknown option '-file1.txt', meaning Git treats the file name as an option.Step 2: Identify correct syntax for stashing specific files
You must use--before file names to separate options from file paths.Final Answer:
You forgot to add--before the file name. -> Option DQuick Check:
Missing--causes unknown option error [OK]
-- before files to avoid option parsing errors [OK]- Omitting
--before file names - Assuming file must be committed first
- Using deprecated stash commands
-app.js, index.html, and style.css. You want to stash only -app.js and style.css, then later apply those changes back. Which sequence of commands correctly does this?Solution
Step 1: Stash specific files with a message
Usegit stash push -m "partial stash" -- -app.js style.cssto stash only selected files with a label.Step 2: Apply the correct stash entry
Usegit stash apply stash@{0}to apply the most recent stash explicitly.Final Answer:
git stash push -m "partial stash" -- -app.js style.css
git stash apply stash@{0} -> Option BQuick Check:
Use-mand--with files, then apply stash by name [OK]
-m for message and -- before files, then apply stash by reference [OK]- Using deprecated
git stash save - Omitting
--before file names - Applying stash without specifying correct stash reference
