Worktrees
Prepare the new worktree
6 min read
A new worktree starts out with only the files tracked by git. What .gitignore ignores is left out:
the .env with the local variables, a development certificate, the dependencies already downloaded.
Without them, the project often won't even run in the new folder.
The Prepare the worktree window solves that in two steps, both optional:
- it copies from the source folder the ignored files that
.worktreeincludepicks; - it runs the repository's setup commands (such as
npm install) in the new folder, with live output and the option to cancel.
.worktreeinclude lives at the root of the repository and uses the same syntax as .gitignore,
including ! for exceptions and ** for any depth. It doesn't pick just any file: only the files
git ignores and that match its patterns go in.
Example:
# the project's local variables
.env
.env.local
# development certificate
certs/dev/*.pem
# each service's local settings, except the example ones
config/**/local.json
!config/exemplo/local.json
Tip
.worktreeinclude is usually committed to the repository, so the whole team
gets the same preparation. It only lists patterns: the files themselves stay ignored and never go
into a commit.
The copy's safety rules:
- it only copies files ignored by git. A tracked file, or one that's merely untracked, never goes in;
- it only copies between folders of the same repository, never outside it;
- shortcuts (symlinks or junctions) are left out, including a folder that points outside the repository;
- nothing that already exists in the new worktree is overwritten;
- there's a ceiling of 20,000 files or 2 GB. Above that, the copy is refused and the window asks for narrower patterns.
The setup commands live in the repository's local configuration (git config --local, the
versora.worktreeSetup key, one value per command). Since that configuration lives in the shared
.git folder, it applies to all the repository's worktrees. It doesn't go to the server with the
push.
The commands run one at a time, in order, inside the new worktree's folder, through the Windows
Command Prompt (cmd). They stop at the first one that fails. Empty lines and lines that start with
# are ignored.
- On its own: the window opens right after a worktree is created, by any path in the app, when
.worktreeincludepicks some file or when there are saved setup commands. Without any of that, it doesn't show up. - A worktree's menu (the left panel's WORKTREES section, or its WIP row in the graph): Prepare (.worktreeinclude and setup)…
- In the Prepare the worktree ‹folder› window, check the Ignored files (.worktreeinclude)
part:
- the "Copy N files (‹size›) into the new worktree" checkbox comes checked when there's something to copy;
- the Files to be copied list shows the files (the first 300, and "and N more files" after that);
- "N files are left out" lists what won't be copied and why.
- Check the Setup command part. The "Commands, one per line (run in sequence in the new folder; stop at the first that fails)" field already comes with the repository's saved commands. You can change them just for this time.
- Leave the Run the setup in ‹folder› checkbox checked to run the commands.
- If you changed the commands and want to keep them for the next worktrees, check Save as this repository's setup commands. The checkbox is only available when the text differs from what's saved.
- Click Prepare. Or click Not now to close without doing anything.
- The window shows "Copying files…" and then the Setup output, live, with each command starting
with a
> commandline. To interrupt, click Cancel. - At the end, the window shows the summary. Use Open the terminal in this worktree to go on in the integrated terminal, already in the new folder, or Close.
| Option | What it does | Default |
|---|---|---|
| Copy N files (‹size›) into the new worktree | Copies the ignored files picked by .worktreeinclude. | Checked when there are files to copy |
| Commands, one per line | The commands to run in the new folder. | The repository's saved commands |
| Run the setup in ‹folder› | Runs the commands after the copy. | Checked when there are commands |
| Save as this repository's setup commands | Saves the current text as the repository's commands. | Unchecked |
To find the files, the app asks git itself what is ignored and what matches .worktreeinclude, and
checks each file again:
git ls-files -z --others --ignored --exclude-standard --directory
git ls-files -z --others --ignored --exclude-from=.worktreeinclude -- <ignored folders>
git check-ignore -z --stdin
The copy is done by the app, file by file, without going through git. The setup commands run as you wrote them. To save the commands:
git config --local --unset-all versora.worktreeSetup
git config --local --add versora.worktreeSetup "npm install"
Preparing doesn't go into Ctrl+Z: it doesn't touch branches, tags or the
staging area. The copied files are ignored files in the worktree's folder. To get rid of them,
delete them or remove the whole worktree. What the setup commands did (for example, the
node_modules folder) is up to them.
- "No .worktreeinclude at the repository root. Create one with .gitignore patterns (e.g. .env) to copy ignored files into new worktrees.": the file doesn't exist. The setup part is still available.
- "The .worktreeinclude doesn't pick any ignored file missing from the new worktree.": the patterns don't match any ignored file, or they all already exist in the new folder.
- "The .worktreeinclude picks N files, over the limit (20,000 files or 2 GB). Narrow down the patterns.": the pattern took too much, for example a whole dependencies folder. Leave that kind of folder to the setup command.
- "Couldn't read the .worktreeinclude.": the file couldn't be read or git refused a pattern.
- Reasons for a file being left out: "invalid path", "it's a link: not copied", "it's outside the repository (through a link)" and "already exists in the new worktree".
- "Ignored files often hold secrets (.env, keys): they're only copied between folders of this repository, never outside it, and nothing that already exists in the new worktree is overwritten.": a reminder that shows up whenever there are files to copy.
- "The setup stopped: "‹command›" exited with code N.": a command failed and the following ones didn't run. Read the output, fix it and run the rest from the terminal.
- "Setup canceled." and "Copy canceled after N files.": you canceled. The running process is ended along with its child processes.
- "Couldn't run the command: ‹error›": Windows couldn't start the command.
Caution
The setup commands run on your machine, as your user. Review what's saved before clicking Prepare, especially in a repository you cloned from someone else.
In the In this repository (‹name›) part of the Worktrees page in Preferences:
- Setup commands: the same field as in the window, with the Save button ("Saved." when it saves). The hint explains: "They run in each new worktree's folder, one per line, after the .worktreeinclude copy (e.g. npm install). They're kept in the repository's local config and apply to all of its worktrees."
- Ignored files to copy: the state of the repository's
.worktreeinclude, for example "The .worktreeinclude picks 3 files (4 KB)." or "This repository has no .worktreeinclude."
With no repository open, the page shows "Open a repository to set up its setup commands."
- Preferences › Worktrees: setup commands and the state of
.worktreeinclude, per repository. - The administrator can hide the Worktrees page in Preferences or make it read-only (the
preferences_hiddenandpreferences_lockedkeys, idworktrees). See Policy keys.
- Create and open worktrees
- Integrated terminal
- Ignoring files
- Official documentation: gitignore (opens another site)