Git Notes
Learn Git worktrees to check out multiple branches at the same time in separate directories. Avoid constant stashing and branch switching.
Git worktrees let you check out multiple branches of the same repository in separate directories simultaneously. Instead of stashing and switching branches, you can have each branch in its own folder.
The Problem Worktrees Solve
Without worktrees, you constantly switch context:
# Traditional painful workflow
git stash # Save current work
git checkout hotfix # Switch to hotfix
# ... fix the bug ...
git checkout feature # Switch back
git stash pop # Restore workWith worktrees, each branch lives in its own directory:
Creating Worktrees
Basic Worktree
# From your main repository
cd ~/projects/my-app
# Create a worktree for the hotfix branch
git worktree add ../my-app-hotfix hotfix/critical-bugPreparing worktree (checking out 'hotfix/critical-bug') HEAD is now at a1b2c3d Fix database connection
Create with a New Branch
git worktree add -b feature/new-api ../my-app-api mainPreparing worktree (new branch 'feature/new-api') HEAD is now at b2c3d4e Latest main commit
Create from Remote Branch
git worktree add ../my-app-review origin/feature/colleague-workListing Worktrees
git worktree list/home/user/projects/my-app a1b2c3d [feature/dashboard] /home/user/projects/my-app-hotfix b2c3d4e [hotfix/critical-bug] /home/user/projects/my-app-api c3d4e5f [feature/new-api]
# Verbose output
git worktree list --porcelainWorking with Worktrees
Each worktree is a fully functional checkout:
# Work in the hotfix worktree
cd ../my-app-hotfix
git status
# Make changes, commit, push - normal Git operations
git add .
git commit -m "Fix critical database bug"
git push origin hotfix/critical-bug
# Go back to your feature work - no stashing needed!
cd ../my-app
# Your feature work is exactly as you left itRemoving Worktrees
# Remove after you are done (clean working directory)
git worktree remove ../my-app-hotfix# No output on success
# Force remove (even with uncommitted changes)
git worktree remove --force ../my-app-hotfix
# If you deleted the directory manually, clean up references
git worktree prunePractical Workflows
Code Review Workflow
# Reviewing a PR without leaving your current work
git fetch origin
git worktree add ../review-pr-42 origin/feature/user-settings
cd ../review-pr-42
# Run tests, review code
npm test
# Done reviewing
cd ../my-app
git worktree remove ../review-pr-42Parallel Development
# Working on frontend and backend simultaneously
git worktree add ../my-app-backend feature/backend-api
git worktree add ../my-app-frontend feature/frontend-ui
# Terminal 1: Backend development
cd ../my-app-backend
npm run dev:server
# Terminal 2: Frontend development
cd ../my-app-frontend
npm run dev:clientTesting Against Main
# Quick comparison against main
git worktree add ../my-app-main main
# Run tests on main to compare
cd ../my-app-main
npm test
# Compare results with your feature branchWorktree Constraints
- A branch can only be checked out in ONE worktree at a time
- Worktrees share the same
.gitobjects (space efficient) - Each worktree has its own staging area and working directory
- Commits in any worktree are visible to all others
# This will fail - branch already checked out
git worktree add ../another-dir mainfatal: 'main' is already checked out at '/home/user/projects/my-app-main'
Interview Questions
- What are Git worktrees and when would you use them?
Worktrees let you check out multiple branches in separate directories simultaneously. Use them when you need to work on multiple branches concurrently — hotfixes, code reviews, or parallel feature development — without stashing or losing context.
- How do worktrees differ from cloning the repository multiple times?
Worktrees share the same .git directory and objects, using minimal extra disk space. Multiple clones duplicate all objects. Worktrees also ensure branch exclusivity — a branch can only be in one worktree.
- Can you have the same branch checked out in multiple worktrees?
No. Git prevents this to avoid conflicting changes. Each branch can only be checked out in one worktree at a time.
- How do you clean up after removing a worktree directory manually?
Run git worktree prune to clean up stale worktree references. This removes entries for worktrees whose directories no longer exist.
- What gets shared between worktrees and what is independent?
Shared: Git objects, refs, hooks, config. Independent: working directory, staging area (index), HEAD. Commits made in any worktree are immediately visible to all others.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Worktrees - Work on Multiple Branches Simultaneously.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, advanced, worktrees, git worktrees - work on multiple branches simultaneously
Related Git & GitHub Topics