Stashing specific files in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using git to stash specific files, it's important to understand how the time to stash grows as the number of files changes.
We want to know how the work git does changes when you stash more or fewer files.
Analyze the time complexity of the following git commands.
git stash push path/to/file1 path/to/file2
# or equivalently
git stash push -- path/to/file1 path/to/file2
This command stashes changes only in the specified files, not all changes in the repository.
- Primary operation: Git scans and processes each specified file's changes.
- How many times: Once per specified file, so the number of files given.
As you stash more files, git does more work linearly with the number of files.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | Processes changes in 10 files |
| 100 files | Processes changes in 100 files |
| 1000 files | Processes changes in 1000 files |
Pattern observation: The work grows directly with the number of files you stash.
Time Complexity: O(n)
This means the time to stash grows in a straight line as you add more files to stash.
[X] Wrong: "Stashing specific files takes the same time no matter how many files I include."
[OK] Correct: Git must check and save changes for each file you list, so more files mean more work and more time.
Understanding how git handles specific files helps you explain efficiency in version control tasks, a useful skill in real projects.
"What if you stash all changes without specifying files? How would the time complexity change?"
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
