Sub topic 9
Untracked files, ignored files and dependencies
This chapter assumes you can create and list linked worktrees, and that you know what .gitignore does. Here you will see what a new worktree contains and what it leaves out. You will also see how to set up the files Git does not track.
What a new worktree contains
A new worktree is a fresh checkout. Git writes only the files that are tracked at the commit you checked out. Everything else in your original directory stays there, and the new worktree does not see it.
That includes:
- Untracked files, such as a scratch note you never added.
- Ignored files, such as
.env,node_modules/and build output. - Uncommitted edits. Changes in the original worktree do not appear in the new one.
The repository history and branches are shared, but the working files are not. Each worktree has its own copy of the project files on disk.
git worktree add ../myproject-hotfix -b hotfix/login-crash main
ls -a ../myproject-hotfixThe listing shows the tracked files and a .git file. It does not show your .env or node_modules/.
Why ignored files are missing
.gitignore tells Git not to track certain paths. Git never commits them, so a checkout cannot restore them. Your local .env exists only in the directory where you created it.
This is the most common surprise after the first git worktree add. The code is there, but the app will not start because its configuration and dependencies are missing.
Setting up a new worktree
You have three practical options for each kind of file.
| File kind | Example | Usual approach |
|---|---|---|
| Local configuration | `.env` | Copy it, or link to it |
| Dependencies | `node_modules/`, `.venv/` | Install again in the new worktree |
| Build output | `dist/`, `target/` | Rebuild, so it matches the branch |
Copy configuration
For a small file that rarely changes, copying is enough.
cp .env ../myproject-hotfix/.envIf you later edit one copy, the other does not change. When you want a single source of truth, use a symbolic link instead.
ln -s "$(pwd)/.env" ../myproject-hotfix/.envA link keeps every worktree on the same settings. Check that your .gitignore covers the link too. A pattern such as .env/ with a trailing slash matches only directories, so it would not ignore a link.
Install dependencies separately
Do not copy or link node_modules/ or a virtual environment between worktrees. Dependencies often differ by branch, and some packages record absolute paths. A virtual environment in particular points at the directory where it was created. Run the install command in the new worktree.
cd ../myproject-hotfix
npm ciThis costs disk space and time, once per worktree. Package managers with a shared cache, such as pnpm, or a warm npm cache make the repeat installs much faster.
Seeing what is only local
Before you remove a worktree, check what would be lost. Inside it, list untracked files, and include ignored ones if you want the full picture.
git status --short
git status --short --ignoredLines that begin with ?? are untracked. Lines that begin with !! are ignored. git worktree remove refuses to delete a worktree that has untracked files unless you pass --force, which is a useful safety check.
Automating the setup
If you create worktrees often, put the steps in a small script so none is forgotten.
#!/usr/bin/env bash
set -euo pipefail
branch="$1"
dir="../$(basename "$(pwd)")-$branch"
git worktree add "$dir" -b "$branch" main
ln -s "$(pwd)/.env" "$dir/.env"
(cd "$dir" && npm ci)Run it as ./new-worktree.sh fix-typo from the root of the original worktree. It assumes a branch named main, a .env file and an npm project, so adjust those to your own setup.
Exercise
You create a worktree for a hotfix, and the app fails to start because it cannot find its database settings. Your original directory has a working .env file, and .env is listed in .gitignore. Why is the file missing, and what are two ways to fix it?
Show answer
Git copies only tracked files into a new worktree. Because .env is ignored, it was never committed and so it stays in the original directory. You can copy it into the new worktree with cp, or create a symbolic link to the original with ln -s so that all worktrees share one file.
Exercise
Which command shows files in a worktree that are ignored, and which marker does it print for them?
Show answer
git status --short --ignored lists them, and ignored files are marked with !!. Untracked files are marked with ??.