Dropping and clearing stashes in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to drop or clear stashes changes as the number of stashes grows.
How does git handle removing one or all stashes when there are many? Git stashes are stored in a reflog file for refs/stash.
Analyze the time complexity of these git commands:
git stash drop stash@{2}
git stash clear
The first command removes a single stash by its index (rewriting reflog). The second removes all at once (deletes files).
Look for repeated steps git must do internally.
- Primary operation: Drop: read entire reflog file (~n lines) and rewrite omitting one entry. Clear: delete two files (refs/stash and logs/refs/stash).
- How many times: Drop processes O(n) lines; clear is fixed file deletions.
As the number of stashes (n) increases, the work changes like this:
| Input Size (n) | Approx. Operations for drop | Approx. Operations for clear |
|---|---|---|
| 10 | About 10 read/write operations | Constant: 2 file deletes |
| 100 | About 100 read/write operations | Constant: 2 file deletes |
| 1000 | About 1000 read/write operations | Constant: 2 file deletes |
Pattern observation: Dropping one stash grows linearly (reflog rewrite), clearing all stays constant.
Time Complexity: O(n) for git stash drop, O(1) for git stash clear
Drop rewrites the O(n)-sized reflog file; clear just deletes files instantly.
[X] Wrong: "Drop is O(1) since it targets one; clear is O(n) iterating all."
[OK] Correct: Reflog is a file; drop rewrites whole file O(n), clear deletes files O(1).
Explaining git internals and why operations scale certain ways demonstrates systems thinking and tool expertise.
"If git used a database or linked structure for reflogs, how might complexities change? Why file-based?"
Practice
git stash drop command do?Solution
Step 1: Understand the purpose of
This command is used to remove one specific stash entry from the stash list.git stash dropStep 2: Differentiate from clearing all stashes
git stash clearremoves all stashes, not just one.Final Answer:
Removes a single stash entry from the stash list -> Option AQuick Check:
Drop = remove one stash [OK]
- Confusing drop with clear
- Thinking drop creates a stash
- Assuming drop shows stash list
Solution
Step 1: Identify the command to clear all stashes
The correct command to remove all stash entries isgit stash clear.Step 2: Check other options for correctness
Options A, B, and C are not valid git commands for clearing stashes.Final Answer:
git stash clear -> Option CQuick Check:
Clear = remove all stashes [OK]
- Using 'drop --all' which is invalid
- Trying 'remove all' which is not a git command
- Confusing delete with clear
stash@{0}: WIP on feature
stash@{1}: Fix bug
stash@{2}: Update docsWhat will be the stash list after running
git stash drop stash@{1}?Solution
Step 1: Understand what
This command removes the stash at index 1, which is 'Fix bug'.git stash drop stash@{1}doesStep 2: Recognize stash reindexing after drop
After dropping stash@{1}, stash@{2} moves up to stash@{1} position.Final Answer:
stash@{0}: WIP on feature
stash@{1}: Update docs -> Option DQuick Check:
Drop stash@{1} removes it and shifts others up [OK]
- Not realizing stash indexes shift after drop
- Assuming stash@{1} remains after drop
- Confusing stash names with indexes
git stash drop without any arguments, but it did not remove all stashes. What is the likely cause?Solution
Step 1: Understand
This command removes one stash: stash@{0} (most recent) if no argument given, or a specific stash if referenced.git stash dropusageStep 2: Identify why it doesn't remove all
Without argument, it only drops the most recent stash, not all. To remove all, repeat or usegit stash clear.Final Answer:
git stash drop without argument removes only the most recent stash (stash@{0}), not all -> Option BQuick Check:
Drop w/o arg = drop stash@{0} only [OK]
- Assuming drop clears all stashes
- Thinking drop without argument does nothing
- Thinking branch affects stash drop
Solution
Step 1: Understand stash indexing and removal
Stashes are indexed from 0 (most recent) upwards. To keep the latest, remove others starting from index 1.Step 2: Use
Dropping stash@{1} repeatedly removes older stashes safely without affecting the latest stash@{0}.git stash drop stash@{1}repeatedlyFinal Answer:
Rungit stash drop stash@{1}repeatedly for all except stash@{0} -> Option AQuick Check:
Drop specific stashes to keep latest safe [OK]
- Using clear which deletes all stashes
- Using pop which applies and removes only one stash
- Dropping stash@{0} which removes the latest stash
