Creating named stashes in Git - Performance & Efficiency
Start learning this pattern below
Jump into concepts and practice - no test required
When using git to save your work temporarily, creating named stashes helps you organize changes. Understanding how the time to create a stash grows with your work size is important.
We want to know how the time to create a named stash changes as the amount of changed files grows.
Analyze the time complexity of the following git command.
git stash push -m "work in progress: feature X"
This command saves your current changes with a custom message, so you can find this stash easily later.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Git scans all changed files to save their current state.
- How many times: Once for each changed file in your working directory.
As you have more changed files, git needs more time to save them all.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 changed files | 10 file saves |
| 100 changed files | 100 file saves |
| 1000 changed files | 1000 file saves |
Pattern observation: The time grows directly with the number of changed files.
Time Complexity: O(n)
This means the time to create a named stash grows linearly with the number of changed files you have.
[X] Wrong: "Creating a named stash takes the same time no matter how many files are changed."
[OK] Correct: Git must save each changed file's state, so more files mean more work and more time.
Understanding how git commands scale with your project size shows you know how tools behave under load. This helps you explain your choices clearly and confidently.
"What if you create a stash with untracked files included? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a stash does
A stash saves your current work temporarily without committing it.Step 2: Recognize the purpose of naming a stash
Giving a stash a name or message helps you remember what changes it contains.Final Answer:
It helps you remember what changes you saved by adding a message. -> Option BQuick Check:
Named stash = clear message [OK]
- Thinking stash commits changes permanently
- Confusing stash with branch creation
- Assuming stash deletes files permanently
Solution
Step 1: Recall the modern syntax for named stashes
The correct command usesgit stash push -m "message"to create a named stash.Step 2: Check each option for correctness
git stash push -m "fix bug" matches the correct syntax exactly; others use outdated or invalid commands.Final Answer:
git stash push -m "fix bug" -> Option DQuick Check:
Named stash command = git stash push -m [OK]
- Using 'git stash save' which is deprecated
- Using 'git stash create' which does not name stashes
- Using 'git stash add' which is invalid
git stash list after running git stash push -m "update readme"?Solution
Step 1: Understand what 'git stash push -m' does
This command creates a stash with the message "update readme".Step 2: Check the format of 'git stash list' output
Named stashes show asstash@{0}: WIP on main: update readme.Final Answer:
stash@{0}: WIP on main: update readme -> Option AQuick Check:
Named stash list shows WIP on branch: message [OK]
- Thinking named stashes lack 'WIP on' prefix
- Thinking stash list is empty after push
- Assuming error when message is given
git stash push -m fix typo but got an error. What is the likely cause?Solution
Step 1: Identify the syntax error in the command
The message contains spaces and must be enclosed in quotes to be treated as one argument.Step 2: Understand correct usage of message option
Using-m "fix typo"is correct; missing quotes causes error.Final Answer:
The message must be enclosed in quotes. -> Option AQuick Check:
Message with spaces needs quotes [OK]
- Omitting quotes around multi-word messages
- Using wrong option like '--message'
- Thinking stash requires committed changes
Solution
Step 1: Understand how to reference named stashes
Named stashes can be referenced usingstash^{/message}syntax to match the message.Step 2: Choose the correct command to apply without removing
git stash applyapplies without removing;popremoves. git stash apply stash^{/feature update} uses correct syntax.Final Answer:
git stash apply stash^{/feature update} -> Option CQuick Check:
Apply named stash with 'stash^{/message}' [OK]
- Using 'pop' which removes stash
- Trying to use '-m' with apply
- Passing message directly without stash^{/}
