mozex/laravel-worktree
Composer 安装命令:
composer require mozex/laravel-worktree
包简介
Create and tear down isolated Laravel Herd git worktrees, each with its own site URL and databases
README 文档
README
Work on a feature branch without touching your main checkout. One command turns a branch into a git worktree that has its own Laravel Herd site, its own application and test databases, and a rewritten .env. A second command finishes the branch, whether that means opening a pull request, merging it, or throwing it away, and then drops the databases and removes the worktree. No leftover databases, no stale .test sites, no shared state between branches.
Read the full documentation at mozex.dev: searchable docs, version requirements, detailed changelog, and more.
Table of Contents
- Installation
- How It Works
- Creating a Worktree
- Finishing a Worktree
- Finding a Worktree
- Configuration
- Warp Terminal
Support This Project
I maintain this package along with several other open-source PHP packages used by thousands of developers every day.
If my packages save you time or help your business, consider sponsoring my work on GitHub Sponsors. Your support lets me keep these packages updated, respond to issues quickly, and ship new features.
Business sponsors get logo placement in package READMEs. See sponsorship tiers →
Installation
Requires PHP 8.2+ - see all version requirements
Install it as a dev dependency:
composer require mozex/laravel-worktree --dev
Publish the config file if you want to change the defaults:
php artisan vendor:publish --tag=worktree-config
That's it. All three Artisan commands are ready to use.
How It Works
Git worktrees let you check out several branches at once, each in its own directory, all backed by one .git folder. That solves the code side of running two branches side by side, but it leaves the environment behind. The new directory has no vendor, no node_modules, no .env, and it points at the same database as your main checkout. Run migrations in one and you've changed the other.
This package fills that gap. worktree:setup runs from your main repository and does the rest of the work for you:
- Creates the worktree next to your project (or wherever you configure).
- Serves it through Herd, so
blogon branchfeature/loginbecomesblog-feature-login.test. - Copies your
.env, then rewrites the database name and every reference to the old host. - Creates a fresh application database and a separate test database, and writes the test database name into
phpunit.xml. - Runs
composer install, migrates the new database, then runs your own extra steps (npm, storage link, whatever you list).
Because it all runs from the main repo, you never cd into a half-built directory. And because it's an Artisan command, it works the same whether you call it by hand, from a Composer script, or from a terminal shortcut.
Creating a Worktree
Pass a branch name:
php artisan worktree:setup feature/login
Leave it off and a branch is generated for you (feature/auto-260714-193000):
php artisan worktree:setup
When you're done you'll see where everything landed:
Worktree ready.
+---------------+------------------------------------------+
| Path | /Users/you/Sites/blog-feature-login |
| Branch | feature/login |
| URL | https://blog-feature-login.test |
| Database | blog_feature_login |
| Test database | blog_feature_login_testing |
+---------------+------------------------------------------+
A few options change what runs:
| Option | What it does |
|---|---|
--base=develop |
Branch off develop instead of the configured base branch |
--seed |
Seed the database after migrating |
--no-migrate |
Create the databases but skip migrations |
--no-database |
Skip databases and PHPUnit entirely |
--no-install |
Skip composer install, plus the migrations and steps that need it |
If the branch already exists, its worktree is checked out as-is instead of branching from scratch.
Finishing a Worktree
When the work is done, run:
php artisan worktree:teardown
It lists your worktrees, asks how you want to finish, and then cleans up. You can skip the questions with flags:
# Push the branch and open a pull request with the GitHub CLI php artisan worktree:teardown feature/login --pr # Merge the branch into main, then clean up php artisan worktree:teardown feature/login --into=main # Throw the branch away php artisan worktree:teardown feature/login --abandon --force
--pr commits any pending changes first, pushes the branch, and opens the PR with gh. Set the commit message with --message="..." if you don't want the default.
Leave --into off and you'll be asked which branch to merge into. Because the merge happens in your main repository, that's the branch you'll be left on afterwards.
Whichever path you pick, the cleanup is the same: drop the application and test databases, unsecure the Herd site, remove the worktree, and delete the branch (except after a pull request, where the branch stays for the open PR). The databases to drop are worked out from the worktree's own name rather than the copied .env, and teardown refuses outright to drop one matching your main repository's DB_DATABASE. Pass --keep-database if you want them left alone.
Finding a Worktree
worktree:path prints where a branch's worktree lives. It creates nothing and touches nothing:
php artisan worktree:path feature/login
# /Users/you/Sites/blog-feature-login
The path is resolved from your config rather than guessed, so it stays correct even after you change path or the host template. That's what makes the cd in the Warp tab configs below land in the right place.
Configuration
Every part of the workflow is driven by config/worktree.php, so the package adapts to your stack instead of forcing one setup. Here are the parts you're most likely to touch.
Herd Modes
The herd option decides how the site is served:
'herd' => env('WORKTREE_HERD', 'secure'),
secureserves the worktree over HTTPS withherd secureand setsAPP_URLtohttps://.linkserves it over HTTP withherd link, which suits a Vite dev server.noneskips Herd, for when you serve the site some other way.
Databases
Each worktree gets its own application database named after the worktree (blog_feature_login) plus a test database with the _testing suffix. The name is lowercased, with anything that isn't a letter or number turned into an underscore, so it stays valid on both MySQL and PostgreSQL. Postgres works too: databases are created against the postgres maintenance connection and dropped WITH (FORCE).
The database.migrate option controls what happens after creation:
'database' => [ 'migrate' => env('WORKTREE_MIGRATE', 'fresh'), ],
fresh runs migrate:fresh, which gives you a clean schema every time, even when you reuse a branch name and its old database is still lying around. Use migrate for a plain migration, or none to handle it yourself.
Host Rewriting
When host.remap_source_host is on, every mention of the old host in the copied .env is repointed at the worktree. So blog.test becomes blog-feature-login.test across APP_URL, mail addresses, and any custom domain keys you keep. Hostnames that only happen to contain the old one, like myblog.test or sub.blog.test, are left alone.
Warp Terminal
If you use Warp, you can wrap these commands in tab configs and get a one-click worktree button with pickers for the repo and branch. Save each block as a .toml file in Warp's tab configs directory, or paste it into Warp's tab config editor.
Create a worktree (drops you into it):
name = "Worktree" color = "green" [[panes]] id = "main" type = "terminal" directory = "{{repo}}" commands = [ '''B="{{branch}}"; php artisan worktree:setup "$B" --base="{{base}}" && [ -n "$B" ] && cd "$(php artisan worktree:path "$B")"''', ] [params.repo] type = "repo" [params.base] type = "branch" default = "main" [params.branch] type = "text" description = "Branch name (leave blank to auto-generate)"
Open an existing worktree:
name = "Worktree Open" color = "blue" [[panes]] id = "main" type = "terminal" directory = "{{repo}}" commands = [ '''cd "$(php artisan worktree:path "{{branch}}")"''', ] [params.repo] type = "repo" [params.branch] type = "branch" description = "Which worktree branch to open?"
Finish a worktree (runs the interactive teardown):
name = "Worktree Finish" color = "yellow" [[panes]] id = "main" type = "terminal" directory = "{{repo}}" commands = [ '''php artisan worktree:teardown''', ] [params.repo] type = "repo"
Each tab leans on worktree:path for the cd, which is why they keep working after a config change.
Resources
Visit the documentation site for searchable docs auto-updated from this repository.
- AI Integration: Use this package with AI coding assistants via Context7 and Laravel Boost
- Requirements: PHP, Laravel, and dependency versions
- Changelog: Release history with linked pull requests and diffs
- Contributing: Development setup, code quality, and PR guidelines
- Questions & Issues: Bug reports, feature requests, and help
- Security: Report vulnerabilities directly via email
License
The MIT License (MIT). Please see the LICENSE file for more information.
mozex/laravel-worktree 适用场景与选型建议
mozex/laravel-worktree 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「git」 「development」 「laravel」 「herd」 「mozex」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mozex/laravel-worktree 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mozex/laravel-worktree 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mozex/laravel-worktree 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Easily manage git hooks in your composer config
Store your language lines in the database, yaml or other sources
Debugging a problem and need to login as one of your customers? This allows you to authenticate as any of your customers.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
MediaWiki extension that allows embedding external content, specified by URL, into your wiki pages
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-15