Bird
Raised Fist0
Gitdevops~15 mins

Worktrees vs stashing decision in Git - Trade-offs & Expert Analysis

Choose your learning style10 modes available
Overview - Worktrees vs stashing decision
What is it?
Worktrees and stashing are two ways to manage unfinished changes in Git. Worktrees let you have multiple working copies of the same repository, each on a different branch or commit. Stashing temporarily saves your changes without committing, so you can switch branches and come back later. Both help you handle work in progress without losing changes.
Why it matters
Without worktrees or stashing, switching branches with unfinished work is risky or impossible without committing incomplete changes. This can slow down development and cause mistakes. These tools let you pause and switch tasks smoothly, improving productivity and reducing errors.
Where it fits
Before this, you should understand basic Git concepts like branches, commits, and the working directory. After learning this, you can explore advanced Git workflows, rebasing, and conflict resolution.
Mental Model
Core Idea
Worktrees create separate workspaces for parallel work, while stashing temporarily shelves changes in one workspace to switch context safely.
Think of it like...
Imagine you have one desk (your main workspace). Worktrees are like having multiple desks in different rooms where you can work on different projects at the same time. Stashing is like putting your current papers in a drawer to clear the desk before starting a new task, then pulling them out later to continue.
Main Repo
  ├─ Worktree 1 (branch A)
  ├─ Worktree 2 (branch B)
  └─ Main working directory

Stash:
  ┌─────────────┐
  │ Unfinished  │
  │ changes     │
  └─────┬───────┘
        ↓
  Saved temporarily
  to be reapplied later
Build-Up - 6 Steps
1
FoundationUnderstanding Git Working Directory
🤔
Concept: Learn what the Git working directory is and how changes live there before committing.
The working directory is where you edit files. Changes here are not saved to Git until you commit. You can switch branches only if there are no conflicting changes.
Result
You know where your changes live and why Git prevents switching branches with uncommitted changes.
Understanding the working directory is key to knowing why tools like worktrees and stash exist to manage unfinished work.
2
FoundationWhat is Git Stash?
🤔
Concept: Introduce stashing as a way to save unfinished changes temporarily.
Git stash saves your current changes in a hidden stack and reverts your working directory to the last commit. You can then switch branches safely and later reapply the saved changes.
Result
You can pause work on one branch and switch to another without losing changes.
Knowing stash lets you manage interruptions without committing incomplete work.
3
IntermediateWhat is Git Worktree?
🤔
Concept: Learn that worktrees let you have multiple working directories linked to the same repository.
Git worktree creates a new folder with a checkout of a branch or commit. You can work on multiple branches simultaneously in separate folders without switching.
Result
You can work on different branches at the same time without stashing or switching.
Worktrees expand your workspace, enabling true parallel work on branches.
4
IntermediateComparing Stash and Worktree Use Cases
🤔Before reading on: do you think stash or worktree is better for quick context switches? Commit to your answer.
Concept: Understand when to use stash versus worktree based on task length and complexity.
Stash is great for short pauses or quick fixes on another branch. Worktrees are better for longer parallel work or when you want to keep multiple branches open simultaneously.
Result
You can choose the right tool based on how long and complex your work interruption is.
Knowing the strengths of each tool helps avoid workflow friction and wasted time.
5
AdvancedManaging Conflicts with Stash and Worktrees
🤔Before reading on: do you think stashing always avoids conflicts when switching branches? Commit to your answer.
Concept: Learn how conflicts can still happen with stash and how worktrees handle them differently.
Stashing saves changes but applying them later can cause conflicts if the target branch changed. Worktrees isolate branches, so conflicts happen only when merging, not switching.
Result
You understand the risk of conflicts with stash and how worktrees reduce that risk.
Knowing conflict behavior prevents surprises and helps plan safer workflows.
6
ExpertPerformance and Storage Implications
🤔Before reading on: do you think worktrees duplicate the entire repository data? Commit to your answer.
Concept: Explore how worktrees share repository data and how stash affects repository size.
Worktrees share the same Git database, so they don't duplicate data but add filesystem overhead. Stashes are stored as commits internally, increasing repository size if used heavily without cleanup.
Result
You can manage disk space and performance when using worktrees and stash extensively.
Understanding internal storage helps optimize resource use in large projects.
Under the Hood
Git worktrees create additional working directories linked to the same .git folder, sharing the repository data but allowing independent checkouts. Stash saves changes as commits in a hidden stack inside the repository, which can be reapplied later. Both manipulate the working directory state but in different ways: worktrees by adding parallel checkouts, stash by temporarily hiding changes.
Why designed this way?
Worktrees were designed to enable true parallel work without switching branches, solving the problem of juggling multiple tasks. Stash was created to quickly save and hide changes without committing, enabling fast context switches. Alternatives like committing unfinished work clutter history, and manual patch files are error-prone.
Repository (.git)
  │
  ├─ Main working directory (branch A)
  ├─ Worktree directory 1 (branch B)
  └─ Worktree directory 2 (branch C)

Stash stack:
  ┌─────────────┐
  │ Stash commit│
  ├─────────────┤
  │ Stash commit│
  └─────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does stashing save untracked files by default? Commit yes or no.
Common Belief:Stashing always saves all changes including new untracked files.
Tap to reveal reality
Reality:By default, stash saves only tracked files; untracked files need special flags to be saved.
Why it matters:Forgetting this causes loss of new files when switching branches, leading to lost work.
Quick: Does creating a worktree duplicate the entire repository data? Commit yes or no.
Common Belief:Worktrees duplicate the whole repository, wasting disk space.
Tap to reveal reality
Reality:Worktrees share the same Git database; only working directories are separate, saving space.
Why it matters:Misunderstanding this can prevent using worktrees due to false concerns about disk usage.
Quick: Can you safely stash changes and switch branches without conflicts every time? Commit yes or no.
Common Belief:Stashing guarantees conflict-free branch switching.
Tap to reveal reality
Reality:Applying stashed changes can cause conflicts if the target branch has incompatible changes.
Why it matters:Assuming no conflicts leads to unexpected merge problems and lost time resolving them.
Quick: Does worktree automatically update when the main branch changes? Commit yes or no.
Common Belief:Worktrees automatically sync with the main branch changes.
Tap to reveal reality
Reality:Worktrees are independent checkouts; changes in one do not affect others until merged.
Why it matters:Expecting automatic sync causes confusion and errors when branches diverge.
Expert Zone
1
Worktrees require careful cleanup; orphaned worktrees can cause repository corruption or confusion.
2
Stash entries are commits and can be inspected, dropped, or applied selectively, enabling advanced workflows.
3
Worktrees can be used to test pull requests or experimental branches without affecting the main workspace.
When NOT to use
Avoid worktrees if your environment restricts multiple directories or if you prefer a single workspace. Avoid stash for long-term storage of changes; use branches instead. For quick fixes, stash is better; for parallel development, worktrees excel.
Production Patterns
Teams use worktrees to handle multiple features simultaneously on local machines. Stash is common for quick context switches during code reviews or hotfixes. Combining both allows flexible workflows: stash for short pauses, worktrees for multitasking.
Connections
Version Control Branching
Worktrees and stashing build on branching concepts by managing changes across branches.
Understanding branches deeply helps grasp why worktrees enable parallel work and stash supports temporary change storage.
Task Switching in Operating Systems
Stashing is like saving process state before switching tasks, worktrees are like running multiple processes simultaneously.
Knowing how OS handles multitasking clarifies why worktrees and stash differ in managing work contexts.
Project Management Kanban Boards
Worktrees represent parallel work lanes, stash represents a paused task queue.
Seeing worktrees and stash as task states helps organize development flow and priorities.
Common Pitfalls
#1Losing untracked files by stashing without saving them.
Wrong approach:git stash # switch branch # untracked files are missing
Correct approach:git stash -u # saves untracked files too
Root cause:Not knowing stash ignores untracked files by default.
#2Leaving many unused worktrees causing confusion and errors.
Wrong approach:git worktree add ../feature-branch feature # worktree left after feature done
Correct approach:git worktree remove ../feature-branch # cleans up worktree properly
Root cause:Forgetting to remove worktrees after use.
#3Applying stash blindly causing conflicts and lost changes.
Wrong approach:git stash apply # conflicts appear unexpectedly
Correct approach:git stash branch new-branch # safely apply stash on new branch
Root cause:Not isolating stash application to avoid conflicts.
Key Takeaways
Worktrees let you work on multiple branches at once by creating separate folders sharing the same repository data.
Stashing temporarily saves your changes in a hidden stack so you can switch branches without committing unfinished work.
Use stash for quick pauses and worktrees for longer parallel development to improve workflow flexibility.
Both tools help avoid committing incomplete changes and reduce errors when switching tasks.
Understanding their differences and limitations prevents common mistakes like lost files, conflicts, and cluttered repositories.

Practice

(1/5)
1. What is the main purpose of git stash in Git?
easy
A. Create a new branch in a separate folder
B. Temporarily save changes and clean the working directory
C. Merge two branches automatically
D. Delete untracked files from the repository

Solution

  1. Step 1: Understand what git stash does

    git stash saves your current changes temporarily and cleans your working directory so you can switch branches or work on something else without committing.
  2. 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.
  3. Final Answer:

    Temporarily save changes and clean the working directory -> Option B
  4. Quick Check:

    Stash = Temporary save changes [OK]
Hint: Stash saves changes fast, worktrees create folders [OK]
Common Mistakes:
  • Confusing stash with branch creation
  • Thinking stash deletes files permanently
  • Mixing stash with merge operations
2. Which command correctly creates a new worktree for branch feature in folder feature-folder?
easy
A. git worktree add feature-folder feature
B. git stash create feature-folder feature
C. git branch add feature-folder feature
D. git checkout -b feature feature-folder

Solution

  1. Step 1: Recall the syntax for adding a worktree

    The correct syntax is git worktree add <path> <branch>. So git worktree add feature-folder feature creates a new folder with the branch checked out.
  2. Step 2: Check other options for correctness

    git stash create is invalid for this purpose. git branch add is not a valid command. git checkout -b creates a branch but does not create a separate folder.
  3. Final Answer:

    git worktree add feature-folder feature -> Option A
  4. Quick Check:

    Worktree add = git worktree add [OK]
Hint: Worktree add uses: git worktree add folder branch [OK]
Common Mistakes:
  • Using stash commands to create worktrees
  • Confusing branch creation with worktree creation
  • Incorrect order of arguments in commands
3. You have uncommitted changes on branch main. You want to switch to feature branch without losing changes. What happens if you run git stash then git checkout feature?
medium
A. Git throws an error and refuses to switch branches
B. Changes are lost permanently after checkout
C. Changes are saved temporarily, branch switches cleanly
D. Changes are automatically merged into feature branch

Solution

  1. Step 1: Understand what git stash does before switching

    git stash saves your uncommitted changes safely and cleans your working directory so you can switch branches without conflicts.
  2. Step 2: Effect of git checkout feature after stash

    Since the working directory is clean after stash, Git switches to feature branch without errors or losing changes. You can later apply the stash.
  3. Final Answer:

    Changes are saved temporarily, branch switches cleanly -> Option C
  4. Quick Check:

    Stash saves changes, checkout switches branch [OK]
Hint: Stash before checkout saves changes safely [OK]
Common Mistakes:
  • Assuming changes are lost after stash
  • Expecting automatic merge of stashed changes
  • Thinking Git errors on switching with stash
4. You tried to create a worktree with git worktree add feature but got an error. What is the most likely cause?
medium
A. The folder name is missing, causing a syntax error
B. You have uncommitted changes that block worktree creation
C. The branch feature does not exist
D. You forgot to stash changes before adding worktree

Solution

  1. Step 1: Check the command syntax

    git worktree add requires two arguments: folder path and branch name. The command git worktree add feature only has one argument, so it is missing the folder name.
  2. 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.
  3. Final Answer:

    The folder name is missing, causing a syntax error -> Option A
  4. Quick Check:

    Worktree add needs folder and branch [OK]
Hint: Worktree add needs folder then branch [OK]
Common Mistakes:
  • Ignoring missing folder argument
  • Assuming uncommitted changes block worktree add
  • Confusing stash necessity with worktree creation
5. You want to work on two branches, main and feature, simultaneously without committing unfinished work on main. Which approach is best?
hard
A. Use git stash to save changes on main and switch branches repeatedly
B. Delete the feature branch and work only on main
C. Commit unfinished work on main before switching to feature
D. Create a worktree for feature branch to work in a separate folder

Solution

  1. Step 1: Understand the need for parallel work

    You want to work on two branches at the same time without committing unfinished work on main. Stashing and switching repeatedly is slow and error-prone.
  2. 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 on main and feature side by side without switching or stashing.
  3. Final Answer:

    Create a worktree for feature branch to work in a separate folder -> Option D
  4. Quick Check:

    Worktrees = parallel branches in folders [OK]
Hint: Worktrees let you work on branches side-by-side [OK]
Common Mistakes:
  • Using stash repeatedly instead of worktrees
  • Committing unfinished work prematurely
  • Deleting needed branches accidentally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard