0
0
Gitdevops~5 mins

Worktrees vs stashing decision in Git - CLI Comparison

Choose your learning style9 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.