git stash to save changes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to save changes with git stash grows as the number of changed files increases.
How does the command handle more changes and how long does it take?
Analyze the time complexity of the following git commands.
git stash save "work in progress"
git stash list
git stash apply stash@{0}
This snippet saves current changes to a stash, lists all stashes, and applies the latest stash.
Look for operations that repeat or scale with input size.
- Primary operation: Saving changes involves scanning all changed files and storing their state.
- How many times: The operation touches each changed file once during stash save.
As the number of changed files grows, the time to save them grows roughly in direct proportion.
| Input Size (changed files) | Approx. Operations |
|---|---|
| 10 | 10 file scans and saves |
| 100 | 100 file scans and saves |
| 1000 | 1000 file scans and saves |
Pattern observation: The time grows linearly as more files are changed.
Time Complexity: O(n)
This means the time to stash changes grows directly with the number of changed files.
[X] Wrong: "Stashing changes takes the same time no matter how many files are changed."
[OK] Correct: The command must process each changed file, so more files mean more work and more time.
Understanding how commands scale with input size shows you can think about efficiency in real tools, a useful skill for any DevOps role.
"What if we stash only specific files instead of all changes? How would the time complexity change?"
Practice
git stash command do?Solution
Step 1: Understand the purpose of
The command saves your current uncommitted changes temporarily without committing them.git stashStep 2: Compare with other git commands
Unlike commit, stash does not save changes permanently; it allows switching tasks without losing work.Final Answer:
Temporarily saves your uncommitted changes to switch tasks -> Option CQuick Check:
git stash = temporary save [OK]
- Thinking stash commits changes permanently
- Confusing stash with branch creation
- Assuming stash deletes files
Solution
Step 1: Identify the modern git stash command
The recommended command to save changes isgit stash push, which explicitly pushes changes to the stash.Step 2: Eliminate incorrect options
git stash saveis deprecated,git stash commitandgit stash addare invalid commands.Final Answer:
git stash push -> Option DQuick Check:
Use git stash push to save changes [OK]
- Using deprecated 'git stash save'
- Trying 'git stash commit' which doesn't exist
- Confusing stash with add or commit commands
git stash push -m "work in progress" git stash list git stash apply
What will be the output of
git stash list?Solution
Step 1: Understand the effect of
This command saves current changes with the message "work in progress" as the latest stash entry.git stash push -m "work in progress"Step 2: Check
Since this is the first stash, it appears asgit stash listoutputstash@{0}: On main: work in progress.Final Answer:
stash@{0}: On main: work in progress -> Option BQuick Check:
First stash is stash@{0} with message [OK]
- Expecting no stash entries after push
- Confusing stash@{0} with stash@{1}
- Assuming apply removes stash entry
git stash push but accidentally included untracked files. Which command fixes this by stashing only tracked files?Solution
Step 1: Understand the problem with untracked files
By default,git stash pushdoes not stash untracked files unless specified.Step 2: Identify the correct option to stash only tracked files
--keep-indexstashes changes but keeps the index intact, effectively ignoring untracked files.Final Answer:
git stash push --keep-index -> Option AQuick Check:
Use --keep-index to stash only tracked files [OK]
- Using --include-untracked adds untracked files instead of excluding
- Assuming --no-untracked or --only-tracked are valid options
- Confusing stash options with git add options
stash@{0}: On main: fix bug
stash@{1}: On main: add featureYou want to apply the older stash (add feature) but keep both stashes after applying. Which command should you use?
Solution
Step 1: Understand difference between apply and pop
git stash applyapplies the stash but keeps it saved;git stash popapplies and removes it.Step 2: Choose command to apply older stash without removing it
Usegit stash apply stash@{1}to apply the older stash and keep both stashes intact.Final Answer:
git stash apply stash@{1} -> Option AQuick Check:
Apply keeps stash, pop removes stash [OK]
- Using pop removes stash entry
- Dropping stash deletes it without applying
- Confusing branch command with apply
