Copy .env and gitignored files into every new git worktree
A new git worktree has only committed files. Copy .env and other ignored files, run npm ci, and give each worktree its own port, with plain git or Sledbot.
Updated 6 min read
On this page
You started Claude Code, Codex or another agent in a new worktree and it can't find its config: no .env, no appsettings.Development.json, no local demo data, and the dev server won't start because node_modules isn't there.
Why gitignored files are missing from a worktree
A worktree is a separate checkout of the same repository. Git populates it from commits only, and files you ignore are not in any commit. So everything listed in .gitignore is absent: environment files, local config, generated folders, dependency folders and local data.
Git has no setting that says "copy these files whenever a worktree is created." Developers work around it with shell aliases, hand-rolled hook scripts or an include list that a script reads. Users of multi-agent tools have also asked for a worktree setup hook that installs dependencies, copies env files and isolates ports. That is the gap this page fills.
There is a second problem that shows up once you run several worktrees at once: they all try to start the dev server on the same port, and the second one fails or silently takes a different port you didn't expect.
| Symptom | Cause | Fix |
|---|---|---|
Agent says .env not found | Ignored file isn't in the checkout | Copy it after creating the worktree |
command not found or missing modules | node_modules is ignored | Run the install command in the worktree |
| Local database or demo data missing | Ignored local data | Copy it, or recreate it in a setup step |
| Port already in use | Every worktree runs the same dev command | Give each worktree its own port |
Copy gitignored files with plain git
If you create worktrees yourself, copy files and install right after creating it:
git worktree add ../myapp-feature -b feature/login
cp .env ../myapp-feature/.env
cd ../myapp-feature && npm ciThis creates the worktree on a new branch, copies one ignored file and installs dependencies. To undo it, run git worktree remove ../myapp-feature from the main checkout. The branch feature/login stays until you delete it.
To make this repeatable in plain git, put the three steps in a small script that takes a branch name and a list of files. It works, but you have to remember to run it, and nothing gives each worktree a separate port.
Copy files and run setup automatically in Sledbot
Sledbot reads a config file at the root of your repo: .sledbot/config.json or sled.json. When it creates a worktree for an agent, it applies the copy and setup entries.
In the repo root, create
.sledbot/config.json(orsled.json).Add the keys you need:
json {"setup": ["npm ci"], "copy": [".env"], "run": "npm run dev -- --port $SLED_PORT", "archive": "docker compose down"}Start an agent with New task (⌘T). Work in a new worktree is on by default for git repos.
Check the new worktree:
.envshould be there andnpm cishould have run.
| Key | What it is for |
|---|---|
setup | A list of commands to run in the new worktree, such as npm ci |
copy | Files to bring into the worktree, such as .env |
run | The command that starts your dev server |
archive | A command that runs when the worktree is archived, such as docker compose down |
The example lists .env as a plain path. List each file you need the same way, for example [".env", "appsettings.Development.json"].
Give each worktree its own port
Terminals in a worktree get these environment variables:
| Variable | Value |
|---|---|
SLED_PORT | The port for this worktree (plus SLED_PORT_1 to SLED_PORT_9 for extra ones) |
SLED_WORKTREE | The worktree |
SLED_ROOT | The repo root |
SLED_BRANCH | The worktree's branch |
SLED_CONTEXT | <worktree>/.sledbot/context (git-excluded) |
Use $SLED_PORT in your run command, as in the example, instead of a hardcoded port. If your app needs a second port (a database admin UI, a websocket server), use SLED_PORT_1 and so on.
Where the worktrees go
- Agents started from New task get a worktree at
<repo>/.sledbot/worktrees/<branch with / replaced by ->. Sledbot adds.sledbot/to.git/info/exclude, so they don't show up as untracked files. - Teammate threads set to "One per thread" use
<parent of repo>/.sled-worktrees/<repo>-<name>-<8 chars>on branchsled/<name>/<8 chars>.
Run the steps from the command line
Every Sledbot terminal has sled on its PATH. The worktree subcommands are info, create, setup, run and archive:
sled worktree create --branch feature/logincreate also accepts --issue N or --pr N instead of --branch, plus --base, --repo, --on (a machine) and --no-setup (skip the setup step). sled thread archive stops an agent, keeps its worktree and a checkpoint ref, and runs the archive script; --no-script skips the script.
Limits to know about
- The copy and setup steps belong to worktrees Sledbot creates. A worktree you made with
git worktree addoutside Sledbot is not covered. - Teammate-thread worktrees live in
<parent>/.sled-worktrees/, while Cleanup's "Sledbot worktrees" category scans<repo>/.sledbot/worktrees/, so those worktrees may not be listed there. - Starting an agent needs a MacSuite license or an active 7-day trial.
Checklist before you try again
- The config file is at the repo root, not in a subfolder.
- The JSON is valid (no trailing comma, no missing quote).
- Each path in
copyexists in your main checkout. - Work in a new worktree is on in New task.
- The folder is a git repository.
- Your
runcommand uses$SLED_PORT, not a fixed number.
If the files still don't appear
- The worktree option was off. Without it the agent works in your existing folder, so no copy happens.
- A listed file doesn't exist in the main checkout. There is nothing to copy; create it there first.
- Two servers still collide. A hardcoded port in
package.jsonor your framework config overrides the one you pass. Check the actual start command. - The agent runs on a remote machine. API keys saved in Sledbot are never copied to remote machines. Set them on the server itself, for example in its shell profile.
If none of that explains it, contact support through the support page with your config file, the output of sled worktree info from the worktree, and what you saw.
Subject: Sledbot worktree not copying .env
Sledbot 1.0.0 on macOS (your version). I started an agent with New task and
"Work in a new worktree" on. My .sledbot/config.json is:
(paste the file)
The new worktree is missing .env. Output of `sled worktree info` is attached.
Steps tried: checked JSON validity, confirmed .env exists in the main checkout.Common questions
Does git worktree copy .gitignore'd files?
No. A worktree only contains tracked files from the commit you check out. Ignored files stay in the original folder.
Can I use symlinks instead of copies?
Plain git lets you symlink an env file with ln -s, so edits stay in sync. Sledbot's copy key is described as bringing files in; the fact sheet doesn't say it links them, so treat it as a copy.
Does this work for Claude Code, Codex and other agents?
Yes. The copy and setup steps apply to the worktree, not to a specific agent, so any agent started there sees the same files.
Does it work on a remote machine?
Agents can run on machines you add over SSH, but keys saved in Sledbot are not copied there. Set secrets on the server itself.