Sub topic 9
Untracked Files, Ignored Files and Dependencies
A new linked worktree is a fresh checkout. It contains the files Git tracks and nothing else. Your .env file, node_modules/, virtual environments and build output stay in the original directory.
This chapter assumes you can create and remove linked worktrees with git worktree add and git worktree remove. It also assumes you know what .gitignore does and which state is shared between worktrees. Here you will learn what a new worktree lacks, how to rebuild it, and which cleanup mistakes lose data.
What a new worktree contains
Git splits the files in a project directory into three groups:
- Tracked files are in the index or in a commit. A checkout writes them to disk.
- Untracked files are in the directory but not tracked, and no ignore rule matches them. Examples are a scratch note or a new source file you haven't run
git addon. - Ignored files match a rule in
.gitignore,.git/info/excludeor your global ignore file. Typical examples are dependencies, compiled output, caches and local secrets.
Only tracked files are written when a worktree is created. The commit history is shared, but untracked and ignored files belong to one directory.
You can see the gap yourself. In the main worktree, list what Git treats as ignored:
git status --short --ignoredLines that start with !! are ignored paths and lines that start with ?? are untracked. Then create a worktree and compare:
git worktree add ../myproject-hotfix -b hotfix/login-crash main
ls -a ../myproject-hotfixEverything marked !! or ?? in the first command will be missing from the second listing. The new directory has your source, but not .env, node_modules/ or dist/. The project will probably not run until you rebuild those.
Why this is usually what you want
The missing files can look like a flaw, but they follow from what a worktree is for. A hotfix worktree starts from a known state: committed code plus whatever setup you add on purpose. Stale build output and half-finished scratch files from another branch can't leak in.
The cost is setup time. For each new worktree you decide how to supply each kind of missing file.
Local configuration and secrets
Files such as .env, .env.local or config/local.yml are ignored because they hold machine-specific values or secrets. You have three options:
- Copy them. This is the simplest approach, and it suits files you won't edit again. Later changes to one copy don't reach the other.
- Symlink them. One file serves every worktree. Edits apply everywhere, which is useful for a shared API key but dangerous if each branch needs different values.
- Generate them. A script or template tool writes the file from a source such as a password manager. This is best for secrets, since nothing is duplicated on disk.
Copying looks like this:
cp ../myproject/.env ../myproject-hotfix/.envA symlink looks like this:
ln -s "$(realpath ../myproject/.env)" ../myproject-hotfix/.envUse an absolute target for the link. A relative target is resolved against the link's own directory, so a path written from your current directory can point to the wrong place.
Dependency directories
Directories such as node_modules/, vendor/, .venv/ and target/ are large and generated. You can handle them in two ways.
Reinstall in each worktree. This is the safest choice. Run npm ci, pip install -r requirements.txt or the equivalent inside the new directory. Modern package managers keep a download cache, so the second install is mostly local copying. pnpm goes further and hard-links packages from one store, so extra node_modules directories cost little disk space.
Share one directory by symlink. This is fast, but it only works while every branch uses the same dependency versions. Once a branch changes package.json, it modifies the shared directory underneath the other worktrees. Avoid sharing directories for that reason.
Never copy a Python virtual environment. A .venv records the absolute path of the interpreter and of its own location, so a copied one breaks or quietly points back at the original. Create a new one:
cd ../myproject-hotfix
python3 -m venv .venv
.venv/bin/pip install -r requirements.txtThe trailing-slash trap with symlinked directories
Suppose .gitignore contains node_modules/ and you symlink that directory into the new worktree. The pattern now fails. A trailing slash in an ignore rule matches only real directories, and a symlink is not one. Git will report the symlink as untracked and may include it in git add ..
Either write the rule without the slash, or list the symlink in .git/info/exclude:
node_modulesWithout the trailing slash, the rule matches both a directory and a symlink named node_modules.
A setup script
If you create worktrees often, put the setup in a script. This one runs from inside the new worktree. It copies local config from a source worktree and installs dependencies when a lockfile exists:
#!/usr/bin/env bash
set -euo pipefail
# The first entry in the porcelain list is the main worktree.
default_source="$(git worktree list --porcelain | awk '/^worktree /{print substr($0, 10); exit}')"
source_tree="${1:-$default_source}"
target_tree="$(git rev-parse --show-toplevel)"
for f in .env .env.local; do
if [ -f "$source_tree/$f" ] && [ ! -e "$target_tree/$f" ]; then
cp "$source_tree/$f" "$target_tree/$f"
echo "copied $f"
fi
done
if [ -f "$target_tree/package-lock.json" ]; then
(cd "$target_tree" && npm ci)
fiThe script never overwrites an existing file. In a bare-repository layout the first entry in the list is the bare repository itself, which has no working files, so pass the source worktree as the first argument.
Git can also run setup for you. git worktree add runs the post-checkout hook, and hooks live in the shared repository, so one hook covers every worktree. On a worktree's first checkout the hook receives an all-zero previous commit ID. That is easy to test for, but the logic is more brittle than an explicit script you run yourself.
Ignored files are deleted when you remove a worktree
This is the data-loss case. git worktree remove refuses to delete a worktree with modified tracked files or untracked files. It does not count ignored files as dirty. A worktree that holds only ignored files is therefore treated as clean, and the remove deletes them.
git worktree remove ../myproject-hotfixIf that worktree had a .env you edited, a local SQLite database or a .venv, they are gone. Before removing a long-lived worktree, check what is ignored in it:
git -C ../myproject-hotfix status --short --ignoredIf you see untracked files, git worktree remove stops and asks for --force. Read the list before you add that flag.
| Kind of file | Present in a new worktree | Blocks `git worktree remove` | Deleted by `git worktree remove` |
|---|---|---|---|
| Tracked, unmodified | Yes | No | Yes |
| Tracked, modified | Yes | Yes | Only with `--force` |
| Untracked | No | Yes | Only with `--force` |
| Ignored | No | No | Yes |
Where ignore rules live
Ignore rules come from several places, and they behave differently across worktrees:
.gitignorefiles are tracked content. Each worktree gets the version from its own checked-out commit, so a branch that edits.gitignorechanges the rules only for that worktree..git/info/excludelives in the shared repository directory. It applies to every worktree. This makes it a good place for personal rules such as an editor's scratch folder or a symlink name.- The global ignore file named by
core.excludesFileapplies to every repository on your machine.
To find out why a path is ignored, ask Git:
git check-ignore -v node_modulesThe output names the file and line number of the matching rule. If it prints nothing, the path isn't ignored.
Other per-directory state
Besides files, some of your tools keep state per directory and need attention in a second worktree:
- Incremental build caches such as
target/,.next/orbuild/start empty, so the first build in a new worktree is a full build. Tools that support a shared cache directory can reduce that cost. - Dev servers listen on a port. Two worktrees running the same default port conflict. Pass a different port to the second one.
- Local databases stored as files are separate per worktree, since they are ignored files. A database server running as a service is shared, so two branches may fight over one schema.
- Editor and IDE settings stored in the project directory, such as
.vscode/or.idea/, are untracked unless committed. Open each worktree as its own project.
Summary of the decision
For each kind of missing file, ask whether the value should be identical across branches. If it should be, a symlink or a single copy is enough. If it can change with the branch, as dependencies and build output can, give each worktree its own copy. Put the steps in a script so you don't repeat them by hand.
Exercise
You create a worktree with git worktree add ../app-review review/pr-42. Running npm test there fails because node_modules/ and .env are missing, even though both exist in your main worktree. Why are they missing, and what is the safest way to fix each?
Show answer
Both are ignored files, and a worktree checkout writes only tracked files. Run npm ci inside the new worktree to get its own node_modules/, because the review branch may have different dependencies. Copy .env from the main worktree (or generate it from your secrets source), because it holds machine-specific values that are the same across branches. Avoid symlinking node_modules/, because a branch that changes dependencies would modify the shared directory.
Exercise
Your .gitignore contains node_modules/. You symlink node_modules into a new worktree, and git status reports it as untracked. Why does this happen, and how do you fix it?
Show answer
A trailing slash in an ignore pattern matches only directories, and a symlink is not a directory for that purpose. Change the rule to node_modules without the slash, or add node_modules to .git/info/exclude, so the rule matches the symlink too.
Exercise
A worktree contains an edited .env (ignored) and no other changes. You run git worktree remove on it without --force. What happens, and what should you have done first?
Show answer
The command succeeds and deletes the directory, including .env, because ignored files don't make a worktree count as dirty. Before removing it, run git -C <path> status --short --ignored to see what is ignored, then copy anything you need to keep.