Supercharge Your Workflow with Git Worktrees and Hooks
Supercharge Your Workflow with Git Worktrees and Hooks
Git worktrees have completely transformed how I manage multiple branches in my development workflow. If you're tired of constantly stashing changes or context-switching between branches, this approach might be just what you need.
What are Git Worktrees?
Git worktrees allow you to check out multiple branches simultaneously in separate directories. Instead of switching between branches in a single working directory, you can have multiple working directories, each with its own branch.
# Basic usage
git worktree add ../feature-branch feature-branch
This command creates a new directory called feature-branch
with that branch
checked out.
My Worktree Workflow
I've developed a workflow where:
- I maintain a "bare" repository with a
.bare
directory as the central git database - My
main
branch lives in a directory namedmain
- Each feature branch gets its own directory
This means I can work on multiple features simultaneously without context switching or stashing changes.
Automation with Scripts
To streamline this process, I've created a few helper scripts:
The New Worktree Script
My new-worktree.sh
script handles:
- Ensuring I'm in the repository root
- Updating the main branch with the latest changes
- Creating a new worktree for a specified branch
- Setting up proper branch tracking
- Configuring git hooks for the new worktree
Git Hooks Support
I've recently added a robust git hooks system to my worktree setup that:
- Maintains a single source of hooks in a shared directory
- Automatically links hooks to each new worktree
- Provides a
setup-hooks.sh
script to configure hooks for existing worktrees
These hooks run checks and tests at different stages:
- Pre-commit: Runs linting and basic tests before each commit
- Pre-push: Performs more thorough checks including build verification
This gives me early feedback about potential issues before they reach CI/CD.
Benefits I've Experienced
- No more stashing: I can leave work-in-progress changes in one branch while working on another
- Faster context switching: Switching between tasks is as simple as changing directories
- Better organization: Each feature gets its own isolated workspace
- Parallel workflows: I can run tests in one worktree while coding in another
- Early feedback: Git hooks run checks and tests before committing or pushing
- Consistent quality: The same validation runs across all worktrees
Limitations
While git worktrees are powerful, they do have some limitations:
- You need to be careful about how you handle dependencies if they change between branches
- IDE integration can sometimes be tricky
- Git hook sharing requires a bit of extra setup (though I've solved this with my scripts)
Getting Started
If you want to try this workflow, check out my scripts in the repository or start with the basic git worktree commands:
# Create a new worktree for an existing branch
git worktree add ../branch-name branch-name
# Create a new branch and worktree in one command
git worktree add -b new-branch ../new-branch main
Setting Up Hooks
For a complete setup with git hooks:
- Create a central hooks directory in your repository root:
mkdir -p hooks
- Add your pre-commit and pre-push hooks to this directory
- Use symbolic links to connect them to each worktree:
ln -sf /path/to/repo/hooks/pre-commit /path/to/worktree/.git/hooks/pre-commit
Or check out my setup-hooks.sh
script that automates this process for all
worktrees.
Give git worktrees a try - they might just revolutionize your development workflow as they did mine!