DEV Community

Cover image for Day 5/30 : Git worktree – Work on multiple branches simultaneously
Ruqaiya Beguwala
Ruqaiya Beguwala

Posted on • Originally published at Medium

Day 5/30 : Git worktree – Work on multiple branches simultaneously

Tired of constantly switching branches and risking merge conflicts? Git Worktrees let you check out multiple branches simultaneously — without the chaos. It allows you to have multiple working trees attached to the same repository.

Step-by-Step Process:

1. Commit your changes in the worktree

# Navigate to your worktree directory cd ../my-worktree # Stage and commit your changes git add . git commit -m "Your commit message" # Push to remote if needed git push origin branch-name 
Enter fullscreen mode Exit fullscreen mode

2. Remove the worktree

Safe method (checks for uncommitted changes):

# From any directory (including the worktree itself) git worktree remove ../my-worktree 
Enter fullscreen mode Exit fullscreen mode

Force method (if you want to discard uncommitted changes):

git worktree remove --force ../my-worktree 
Enter fullscreen mode Exit fullscreen mode

3. Clean up (optional)

# Prune any stale worktree references git worktree prune 
Enter fullscreen mode Exit fullscreen mode

Practical Use Cases

Bug Fixing While Developing:

Have a bug that need to be fixed urgently? Usually, you have to stash our existing changes, but not anymore!

# Main worktree: working on new feature git checkout feature/new-dashboard # Create worktree for urgent bugfix git worktree add ../hotfix-branch hotfix/login-issue 
Enter fullscreen mode Exit fullscreen mode

Testing Different Versions:

Suppose you want to check what changes in prior version:

# Compare version 1.0 with current development git worktree add ../v1.0 v1.0.0 
Enter fullscreen mode Exit fullscreen mode

Long-Running Tasks:

Suppose, you want to run test cases in a different branch, but want to continue development in your current branch:

# Run tests in one worktree while coding in another git worktree add ../test-runner test/feature-x cd ../test-runner npm test # runs in background cd ../main-repo # continue coding 
Enter fullscreen mode Exit fullscreen mode

Tips and Tricks

  • Detached HEAD Worktrees: Instead of branches, you can create worktree out of a commit:
# Create worktree for specific commit (not a branch) git worktree add --detach ../investigate abc1234 
Enter fullscreen mode Exit fullscreen mode
  • Track Remote Branches Easily: Pull from a particular remote branch as and when required:
git worktree add -b new-feature ../new-feature origin/new-feature 
Enter fullscreen mode Exit fullscreen mode
  • Clean Up: Clean your worktree once the task is completed.
# After finishing with a worktree: git worktree remove ../myfeature # To clean up stale entries: git worktree prune 
Enter fullscreen mode Exit fullscreen mode
  • Performance Tip: Worktrees share the same Git objects, so they're much faster than cloning but remember that operations like git gc will affect all worktrees.
  • IDE Friendly: Many modern IDEs (VSCode, IntelliJ) can detect and work with Git worktrees seamlessly.
  • Temporary Worktrees: Great for quick experiments:
git worktree add -b temp-experiment ../temp # When done: rm -rf ../temp git worktree prune git branch -d temp-experiment 
Enter fullscreen mode Exit fullscreen mode

Conclusion

Git worktrees can be a game-changer for developers juggling multiple branches or projects. Whether you're reviewing a PR or prototyping a new feature, worktrees streamline your workflow. Give them a try — you’ll thank yourself later.

Pro tip:

You can use Git Worktree Manager in VSCode extensions to make the task seamless.


Up Next: git submodule – Managing dependencies inside a repo


Daily advance GIT tips in your inbox—worth starting? Respond to my poll here🚀
For more useful and innovative tips and tricks, Let's connect on Medium

Top comments (0)