Git Worktrees

Advanced Git

Amanda Crawford-Adamo

Software and Data Engineer

What is a Git Worktree?

Git Worktree Command

git worktree
  • Can "checkout" multiple branches in your workspace.
  • Similar to a repo checkout, but efficient
  • No need for stashing changes
  • No need to switch between branches during development

A fake tree that's composed of circles and lines

Advanced Git

Git Worktree versus Git Switch

This tables compares using git worktree vs git switch in a development workflow. $$

Git Worktree Git Switch
Multiple active branches One active branch at a time
Separate directories Single working directory
No need to stash changes May require stashing
Advanced Git

Creating a Git Worktree

Create new work tree from <branch> into directory <path>

git worktree add <path> <branch>

Example

Create a new work tree from the bugfix/data-validation branch into the ../etl-bugfix directory

git worktree add ../etl-bugfix bugfix/data-validation
Advanced Git

Listing and Removing Worktrees

  • Lists all active worktrees: git worktree list

Example Output

$ git worktree list
flight-pipeline            a1b2c3d [main]
flight-pipeline-feature    e4f5g6h [feature]
flight-pipeline-hotfix     i7j8k9l [hotfix]
  • Removes a worktree from a <path>: git worktree remove <path>

Example Output

$ git worktree remove flight-pipeline-hotfix
flight-pipeline-hotfix: deleted
Advanced Git

When to use Git Worktrees

When to use:

  • Working on multiple features simultaneously
  • Handling urgent bug fixes without disrupting ongoing work
  • Running tests on different branches in parallel
  • Code reviews while continuing development

Reconsider when:

  • Disk space is limited
  • Projects with frequent updates and complex merge
Advanced Git

Best practices for Git Worktrees

When using Git worktrees, keep these tips in mind:

  1. Use clear naming conventions for worktree directories
  2. Regularly prune unused worktrees to keep the workspace clean
  3. Be mindful of disk space, especially with large projects
  4. Use worktrees for short-lived parallel work to avoid confusion
Advanced Git

Let's practice!

Advanced Git

Preparing Video For Download...