Bird
Raised Fist0
Gitdevops~5 mins

Worktrees vs stashing decision in Git - CLI Comparison

Choose your learning style10 modes available
Introduction
Sometimes you need to switch tasks quickly without losing your current work. Git offers two ways to handle this: worktrees let you work on multiple branches at once in separate folders, while stashing temporarily saves your changes to come back to later.
When you want to work on two different features at the same time without mixing files.
When you need to switch branches but have unfinished changes you don't want to commit yet.
When you want to test a quick fix on another branch without disturbing your current work.
When you want to keep your workspace clean but save your progress to return to later.
When you want to avoid committing incomplete or experimental changes to the repository.
Commands
This command saves your current changes safely and cleans your working directory so you can switch branches without losing work.
Terminal
git stash
Expected OutputExpected
Saved working directory and index state WIP on main: abc1234 Commit message
Shows all the saved stashes so you can see what changes you have stored.
Terminal
git stash list
Expected OutputExpected
stash@{0}: WIP on main: abc1234 Commit message
Restores the most recent stash and removes it from the stash list, bringing your saved changes back into your working directory.
Terminal
git stash pop
Expected OutputExpected
On branch main Changes not staged for commit: modified: file.txt Dropped refs/stash@{0} (abc1234 Commit message)
Creates a new folder with a separate working copy of the specified branch so you can work on it independently without affecting your current folder.
Terminal
git worktree add ../feature-branch feature-branch
Expected OutputExpected
Preparing worktree (checking out 'feature-branch') HEAD is now at def5678 Another commit message
Lists all the worktrees you have, showing their paths and the branches checked out in each.
Terminal
git worktree list
Expected OutputExpected
/path/to/repo abc1234 [main] /path/to/feature-branch def5678 [feature-branch]
Key Concept

If you remember nothing else, remember: use stashing to save and switch quickly in one folder, and use worktrees to work on multiple branches side-by-side in separate folders.

Common Mistakes
Trying to switch branches with uncommitted changes without stashing or using worktrees.
Git will prevent switching branches to avoid losing changes or causing conflicts.
Use 'git stash' to save changes or create a worktree to work on another branch separately.
Forgetting to pop or apply a stash after switching back.
Your saved changes remain hidden and you might lose track of them.
Run 'git stash pop' or 'git stash apply' to restore your saved work.
Creating too many worktrees without cleaning them up.
It can clutter your disk and cause confusion about which worktree is active.
Remove unused worktrees with 'git worktree remove <path>' when done.
Summary
Use 'git stash' to save unfinished changes temporarily and switch branches safely in the same folder.
Use 'git worktree add' to create separate folders for working on multiple branches at the same time.
'git stash pop' restores saved changes, and 'git worktree list' shows all active worktrees.

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