Worktrees vs stashing decision in Git - Performance Comparison
When deciding between using git worktrees or stashing changes, it's important to understand how the time to complete these actions grows as your project size or number of changes increases.
We want to know how the effort and time scale when switching contexts with worktrees versus stashing.
Analyze the time complexity of these git commands:
# Using stash
$ git stash save "WIP changes"
$ git checkout other-branch
$ git stash pop
# Using worktree
$ git worktree add ../other-branch-dir other-branch
# Work in new directory without affecting current one
This snippet shows saving changes with stash and switching branches versus creating a new worktree for parallel work.
Look at what repeats or scales with input size:
- Primary operation: Saving and applying changes (stash) or creating a new worktree directory.
- How many times: Each stash save/pop involves compressing and decompressing changes; worktree creation involves linking files once.
As the number of changed files or size of changes grows:
| Input Size (changed files) | Approx. Operations (stash) |
|---|---|
| 10 | Quick compress and decompress |
| 100 | Noticeable delay saving and applying stash |
| 1000 | Longer time compressing and applying stash |
| Input Size (repo size) | Approx. Operations (worktree) |
|---|---|
| 10 files | Quick directory setup with links |
| 100 files | Still fast due to linking, minimal copying |
| 1000 files | Setup time grows slowly, mostly filesystem metadata operations |
Pattern observation: Stashing time grows with change size because of compression work; worktree setup grows slowly with repo size due to filesystem operations.
Time Complexity: O(n)
This means the time to stash or create a worktree grows roughly in direct proportion to the number of changed files or repo size.
[X] Wrong: "Stashing is always faster than creating a worktree regardless of project size."
[OK] Correct: Stashing involves compressing changes which can slow down as changes grow, while worktrees mostly create links and metadata, often faster for large projects.
Understanding how git operations scale helps you explain your choices clearly and shows you think about efficiency, a valuable skill in real projects.
What if we changed from using git stash to git stash with patch mode? How would the time complexity change?
Practice
git stash in Git?Solution
Step 1: Understand what
git stashdoesgit stashsaves your current changes temporarily and cleans your working directory so you can switch branches or work on something else without committing.Step 2: Compare with other options
Creating a new branch in a separate folder is done with worktrees, not stash. Merging branches and deleting untracked files are unrelated commands.Final Answer:
Temporarily save changes and clean the working directory -> Option BQuick Check:
Stash = Temporary save changes [OK]
- Confusing stash with branch creation
- Thinking stash deletes files permanently
- Mixing stash with merge operations
feature in folder feature-folder?Solution
Step 1: Recall the syntax for adding a worktree
The correct syntax isgit worktree add <path> <branch>. Sogit worktree add feature-folder featurecreates a new folder with the branch checked out.Step 2: Check other options for correctness
git stash createis invalid for this purpose.git branch addis not a valid command.git checkout -bcreates a branch but does not create a separate folder.Final Answer:
git worktree add feature-folder feature -> Option AQuick Check:
Worktree add = git worktree add [OK]
- Using stash commands to create worktrees
- Confusing branch creation with worktree creation
- Incorrect order of arguments in commands
main. You want to switch to feature branch without losing changes. What happens if you run git stash then git checkout feature?Solution
Step 1: Understand what
git stashdoes before switchinggit stashsaves your uncommitted changes safely and cleans your working directory so you can switch branches without conflicts.Step 2: Effect of
Since the working directory is clean after stash, Git switches togit checkout featureafter stashfeaturebranch without errors or losing changes. You can later apply the stash.Final Answer:
Changes are saved temporarily, branch switches cleanly -> Option CQuick Check:
Stash saves changes, checkout switches branch [OK]
- Assuming changes are lost after stash
- Expecting automatic merge of stashed changes
- Thinking Git errors on switching with stash
git worktree add feature but got an error. What is the most likely cause?Solution
Step 1: Check the command syntax
git worktree addrequires two arguments: folder path and branch name. The commandgit worktree add featureonly has one argument, so it is missing the folder name.Step 2: Understand error cause
Missing folder argument causes Git to throw a syntax error. Uncommitted changes or branch existence issues cause different errors, but syntax error is most likely here.Final Answer:
The folder name is missing, causing a syntax error -> Option AQuick Check:
Worktree add needs folder and branch [OK]
- Ignoring missing folder argument
- Assuming uncommitted changes block worktree add
- Confusing stash necessity with worktree creation
main and feature, simultaneously without committing unfinished work on main. Which approach is best?Solution
Step 1: Understand the need for parallel work
You want to work on two branches at the same time without committing unfinished work onmain. Stashing and switching repeatedly is slow and error-prone.Step 2: Choose the best tool for parallel branch work
Worktrees allow you to have multiple branches checked out in different folders simultaneously. This lets you work onmainandfeatureside by side without switching or stashing.Final Answer:
Create a worktree forfeaturebranch to work in a separate folder -> Option DQuick Check:
Worktrees = parallel branches in folders [OK]
- Using stash repeatedly instead of worktrees
- Committing unfinished work prematurely
- Deleting needed branches accidentally
Ready to prove it?
Pass the test and this pattern moves unknown
