Rails Worktree
Git worktree management for Rails projects with isolated databases and configurations.
Features
- Create git worktrees with automatic branch creation
- Isolated databases per worktree (separate development and test databases)
- Automatic configuration file copying
- Copy node_modules from main worktree
- Easy cleanup with database dropping and directory removal
Installation
Add to your Gemfile:
group :development do
gem "rails-worktree"
end
Run bundle install. A binstub will be automatically created at bin/worktree.
Manual binstub installation (if needed):
bundle exec rake worktree:install
Usage
Create a worktree
bin/worktree feature-branch # From current branch
bin/worktree feature-branch main # From specific branch
This creates a new worktree with:
- Separate databases:
myapp_feature-branch_developmentandmyapp_feature-branch_test - Copied configuration files (
.env,database.yml,Procfile.dev, credentials) - Copied
node_modules - Migrated and seeded databases
Close a worktree
bin/worktree --close feature-branch # From main repo
bin/worktree --close # From within worktree
Drops both databases, removes the directory, deletes the branch, and cleans up git references.
Database Configuration
The gem uses environment variables for database names:
DATABASE_NAME_DEVELOPMENT- Development database nameDATABASE_NAME_TEST- Test database name
Your config/database.yml should look like:
development:
<<: *default
database: <%= ENV.fetch("DATABASE_NAME_DEVELOPMENT", "myapp_development") %>
test:
<<: *default
database: <%= ENV.fetch("DATABASE_NAME_TEST", "myapp_test") %>
The gem automatically:
- Sets these variables in the worktree's
.envfile - Updates the worktree's
database.ymlto use them - Creates both development and test databases with unique names
Requirements
- Ruby >= 2.6.0
- Rails project with standard structure
- PostgreSQL (or modify for your database)
License
MIT