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
2. Remove the worktree
Safe method (checks for uncommitted changes):
# From any directory (including the worktree itself) git worktree remove ../my-worktree
Force method (if you want to discard uncommitted changes):
git worktree remove --force ../my-worktree
3. Clean up (optional)
# Prune any stale worktree references git worktree prune
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
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
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
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
- 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
- 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
- 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
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)