工作树初始化器
 ](https://www.nuget.org/packages/WorktreeInitializer) 
一种用于将gignored文件从源代码存储库复制到新工作树的工具。既可作为CLI工具,又可作为AI代理的MCP(模型上下文协议)服务器。
当你创建git工作树时,git忽略了以下文件 .env 文件夹, launchSettings.json 有秘密, node_modules/,构建输出和其他本地配置不包括在内。此工具在一个命令中复制所有这些内容。
安装
dotnet tool install --global WorktreeInitializer安装后 wi 命令将在全球范围内可用。
要更新到最新版本:
dotnet tool update --global WorktreeInitializer要卸载,请执行以下操作:
dotnet tool uninstall --global WorktreeInitializer要在Claude Code中将其注册为MCP工具:
claude mcp add worktree-init -- wi --mcp对于Cursor或其他MCP客户端,请将以下内容添加到MCP配置中:
{
"mcpServers": {
"worktree-init": {
"command": "wi",
"args": ["--mcp"]
}
}
}用法
wi init [--ignore
]... [--include
]...
wi init [--ignore
]... [--include
]...
wi help例子
# Auto-detect: run inside a worktree to automatically find the source repo
cd ~/code/myproject-worktree
wi init
# Explicit paths: specify source and destination directly
wi init C:\code\myproject C:\code\myproject-worktree
# Exclude specific directories from copying
wi init ./myproject ./myproject-worktree --ignore node_modules --ignore .venv
# Re-include a path that would otherwise be ignored (include wins over ignore)
wi init ./myproject ./myproject-worktree --ignore node_modules --include node_modules
# Auto-detect with flags
wi init --ignore node_modules --ignore .venv
# Works with paths containing spaces (just quote them)
wi init "/home/user/my project" "/home/user/my project-wt"
# Unix paths work the same way
wi init ~/code/myproject ~/code/myproject-worktree自动检测模式
当你奔跑时 wi init 由于git工作树中没有路径,它会自动检测主存储库作为源,并使用当前目录作为目标。这是使用该工具的最简单方法——只是 cd 走进你的工作台,然后跑 wi init.
WorktreeConfig.json
你可以放置一个 WorktreeConfig.json 在源代码仓库根目录中定义默认忽略的文件:
{
"ignores": ["node_modules", ".venv", "dist"]
}- 该文件是可选的——如果缺少,则不会应用默认忽略值
- 如果存在但格式错误,则报告错误
- 命令行界面
--ignore标志与配置文件合并忽略(两者的联合) --include覆盖两个CLI--ignore配置文件忽略(包括总是赢)
行为
使用git发现被忽略的文件
跑 git ls-files --others --ignored --exclude-standard 在源目录中。这可以处理所有 .gitignore 复杂性:嵌套 .gitignore 文件、全局gitignore、, .git/info/exclude否定模式和其他所有gitignore特征。
保留目录结构
文件被复制到目标中的相同相对路径。如果 src/bin/Debug/app.dll 在源中被忽略,它将被复制到 src/bin/Debug/app.dll 在目的地。
根据需要创建目录
如果目标目录结构尚不存在,则会在复制过程中自动创建。
覆盖现有文件
如果目标中已存在文件,则会覆盖该文件。这样可以安全地重新运行 wi init 如果源文件已更改。
报告部分故障
如果某些文件无法复制(例如被另一个进程锁定),其余文件仍会被复制。输出列出了哪些文件失败以及原因。
PATH中需要git
由于该工具使用 git ls-files 要发现被忽略的文件,必须安装git并在PATH中可用。如果你使用的是git工作树,那么你已经安装了git。
作为MCP服务器
wi --mcp当作为MCP服务器运行时,可以使用以下工具:
wi_init(sourcePath?, destinationPath?, ignorePaths?, includePaths?)-将所有gignored文件从源复制到目标(当省略路径且服务器在工作树内运行时,会自动检测路径)wi_help()-获取帮助
发展
git clone
cd worktree-initializer
dotnet build WorktreeInitializer.slnx
dotnet test WorktreeInitializer.slnx在开发过程中作为MCP服务器运行:
dotnet run --project WorktreeInitializer.Cli/WorktreeInitializer.Cli.csproj -- --mcp要打包:
dotnet pack WorktreeInitializer.Cli/WorktreeInitializer.Cli.csproj --configuration Release许可证
MIT许可证
