Boost Your Productivity with Git Worktrees
Branch out like a tree — work on multiple features at once without losing your roots, thanks to Git worktrees! 🌿🚀
Introduction
I have been using Git ever since I stepped into software development. It’s an essential tool for version control, but managing multiple tasks within a single repository can still be daunting task. I often face issues such as:
- Working on multiple features simultaneously, creates confusion with which branch had what changes and whats stashed where
- Handling urgent bug fixes while in the middle of complex changes or feature implementation, meaning creating a complete context switch and first I’ll have to either stash or commit work in progress changes
- Reviewing code without disrupting current work and often creating local tags for my peers so they can use them as library versions for further testing
- Testing experimental changes or continuing on something that was previously blocked due to things not in my control
After facing such issues for almost 3 years in the industry, I was taken aback on finding about “Git worktrees”. It not only provides a powerful solution to these problems by allowing multiple working directories for a single repository, but also enables seamless multi-tasking without constantly stashing or switching branches. Seriously what a life saver!
In this small blog post, I go through everyday scenarios that I use “Git worktrees” in along with a small explanation on what it is.
What are Git Worktrees?
Git worktrees allow you to check out multiple branches at the same time within separate directories, all linked to the same repository. Each worktree acts as an independent working directory but shares the same .git directory, avoiding the need for multiple clones.
Key Concepts:
- Main worktree: The original working directory created with git init or git clone
- Linked worktrees: Additional worktrees created with git worktree add
- Shared references: refs/ are shared across worktrees, while pseudo refs are worktree-specific
Practical Use Cases and Examples
Efficient Multi-Branch Development
Scenario:
I often find myself fixing bugs or catering to urgent product/design requirement changes on really short notices. Lets say we are working on a feature in branch feature-a, but a hotfix needs to be applied to branch main immediately for a release. Instead of stashing or committing my unfinished work, I can simply create a new worktree for main.
Steps:
git worktree add ../hotfix main cd ../hotfix # Apply fix, commit, and push
Once the hotfix is completed, we return to our feature branch and continue with its development:
cd ../your-original-repo
Parallel Development
Scenario:
We find ourselves working on multiple features at once and switching between them becomes a hassle. Lets say we were working on a feature feature-a which is now under review by a peer, but we also need to start work on feature-b . We can’t switch branches because we also need to cater to any PR comments that come our way. So we create a separate worktree for our new feature to live in.
Steps:
git worktree add ../feature-b-path feature-b-branch cd ../feature-b
This allows us to continue development without waiting for merges or rebases. Also allowing us to resolve those PR comments easily by switching back to the previous worktree, without the need to stash or commit our work in progress changes on the new feature.
Code Reviews
Scenario:
So lets say our peer finished up their amazing, ground breaking, revolutionizing feature (😮💨) they have been working on for months, and they asked us for a review. We find the PR is about a 100 files being updated and a few thousand lines of code changes. As usual we are lazy, we can’t review each line of code (it’s daunting) and reviewing PR still does not ensure if the project even runs anymore. So we create a new worktree where we switch to their PR, run the project, check a few things and approve it.
Steps:
git fetch origin pull/123/head:pr-123 git worktree add ../pr-123 pr-123 cd ../pr-123 # Review code
After finishing the review, we remove the worktree:
git worktree remove ../pr-123
Creating a New Worktree for Feature Development
Scenario:
Another scenario we commonly face is when we want to work on new feature without affecting the current branch.
Steps:
git worktree add -b new-feature-branch ../new-feature-path cd ../new-feature # Start coding on new-feature branch
Managing Multiple Worktrees
Scenario:
You need to keep track of active worktrees? Sure thing, we can simply list them down.
Steps:
git worktree list
This will display paths, branches, and statuses of all worktrees.
Cleaning Up Unused Worktrees
Scenario:
We have finished work on a branch and no longer need the worktree.
Steps:
git worktree remove ../feature-branch
This removes the worktree directory while keeping the branch intact in the repository.
Updating a Worktree
Scenario:
You want to ensure your worktree is up to date with the latest changes from the main branch.
Steps:
cd ../feature-branch git pull origin main --rebase
This will update your branch with the latest changes.
Tips and Tricks
- Use clear and descriptive directory names to keep track of different worktrees.
- Regularly check active worktrees with:
git worktree list
- Remove worktrees when no longer needed to free up space:
git worktree remove <path>
- If a branch is already checked out elsewhere in a worktree worktree-a but you need to checkout that branch in the current worktree worktree-main, first switch to another branch or remove the existing worktree worktree-a.
Conclusion
Git worktrees have significantly enhanced my productivity at work by enabling me to-do efficient multi-tasking, isolating workspaces for clearer context switching, and seamless branch management so I always remain unblocked. Whether you’re handling bug fixes, experimenting, or reviewing code, worktrees provide a robust alternative to traditional branch switching and will make your life a lot easier.
I recommend you try incorporating worktrees into your workflow and make software development even more delightful for yourself and your peers.
Loved this article?
Hit the like button
Share this article
Spread the knowledge
More from the world of CARS24
The Art of Intuition in Tech Leadership
Experience sharpens this instinct, but it requires introspection. While excitement fuels innovation, true breakthroughs come when leaders spend more time thinking deeply and executing with precision.
Caching in ReactNative apps — Boon or Bane?
These tools ensure efficient data handling, reducing load times and enhancing user experience while avoiding stale data issues. Touching point on few strategies that I commonly use as a practice.
Bharat RTO: Transforming Saadhan into the Future of Driving Convenience
Bharat RTO is designed to simplify your interactions with RTO services—everything under one roof.