Listing worktrees in Git - Time & Space Complexity
When using Git, listing worktrees shows all linked working directories. Understanding how the time to list worktrees grows helps us know how fast this command runs as projects grow.
We want to know how the command's work changes as the number of worktrees increases.
Analyze the time complexity of the following Git command.
git worktree list
This command lists all the worktrees linked to the current Git repository.
Look for repeated steps the command does internally.
- Primary operation: Iterating over each worktree entry stored in the repository metadata.
- How many times: Once for each worktree configured (n times).
The command reads and processes each worktree entry one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads and prints |
| 100 | 100 reads and prints |
| 1000 | 1000 reads and prints |
Pattern observation: The work grows directly with the number of worktrees; doubling worktrees doubles the work.
Time Complexity: O(n)
This means the time to list worktrees grows linearly with how many worktrees exist.
[X] Wrong: "Listing worktrees is instant no matter how many worktrees there are."
[OK] Correct: The command must read each worktree's info, so more worktrees mean more work and time.
Understanding how commands scale with input size shows you can reason about tool performance, a useful skill in real projects and interviews.
"What if the worktree info was cached in a single file? How would that affect the time complexity?"
Practice
git worktree list do?Solution
Step 1: Understand the purpose of
This command lists all worktrees connected to the repository, showing their folder paths and branches.git worktree listStep 2: Compare with other options
Options A and C describe deleting or creating worktrees, which are done by different commands. Shows the commit history of the current branch shows commit history, unrelated to worktrees.Final Answer:
Shows all the linked worktrees with their paths and branches -> Option DQuick Check:
List worktrees = B [OK]
- Confusing 'list' with 'add' or 'remove' commands
- Thinking it shows commit history
- Assuming it deletes worktrees
Solution
Step 1: Recall the exact command for listing worktrees
The correct command isgit worktree listto display all worktrees.Step 2: Verify other options
Options A, B, and D are invalid or do not exist in git worktree commands.Final Answer:
git worktree list -> Option CQuick Check:
Correct syntax for listing = C [OK]
- Using 'show' or 'status' instead of 'list'
- Shortening 'list' to 'ls' which is invalid
- Confusing with other git commands
git worktree list below, what branch is checked out in the worktree at /projects/myrepo/feature?
/projects/myrepo 123abc [main] /projects/myrepo/feature 456def [feature-branch]
Solution
Step 1: Identify the worktree path in the output
The path/projects/myrepo/featurecorresponds to the second line in the output.Step 2: Read the branch name in brackets
The branch checked out there isfeature-branch, shown in square brackets.Final Answer:
feature-branch -> Option BQuick Check:
Branch at /feature = feature-branch [OK]
- Confusing commit hashes with branch names
- Picking the wrong line for the path
- Ignoring the brackets around branch names
git worktree list but got an error: fatal: 'worktree' is not a git command. What is the most likely cause?Solution
Step 1: Understand the error message
The error means git does not recognize the 'worktree' command, likely due to version.Step 2: Check git version support
Worktrees were introduced in git 2.5+. Older versions lack this command.Final Answer:
Your git version is too old and does not support worktrees -> Option AQuick Check:
Unsupported git version = A [OK]
- Assuming typo in command name
- Thinking worktrees need special init
- Not verifying git version
Solution
Step 1: Understand git worktree list filtering options
Git worktree list does not have built-in branch filtering options like --branch or --filter.Step 2: Use shell tools to filter output
Usinggrep releaseafter listing worktrees filters lines containing 'release', showing only those branches.Final Answer:
git worktree list | grep release -> Option AQuick Check:
Filter branches by grep after list = D [OK]
- Assuming git worktree list supports --branch or --filter
- Using 'show' instead of 'list'
- Expecting git to filter output internally
Ready to prove it?
Pass the test and this pattern moves unknown
