Git Worktree Add: Create and Run a Second Checkout

Use git worktree add to create a second checkout, start two branches on different local ports, and verify which branch each URL actually serves.

Light technical illustration contrasting a loopback port collision with two live Git worktree servers that return different branch markers.

Use git worktree add -b fix/login ../app-fix-login main to create a new branch and check it out in a second directory. Use git worktree add ../app-review review/login when the branch already exists. Your original checkout can stay where it is. Before you start editing, verify the new directory's branch and commit. Before you run the app, give that copy its own port and confirm that the response actually comes from the branch you meant to start.

In our disposable local test, Git put the new checkout on the requested branch and kept staged changes in its own index. Starting a second server on the original port failed with Address already in use. The first server still answered. When we started the second on another port, the response from each URL identified the expected branch. Those are different checks, and both belong in the setup.

Add a worktree and verify the starting point

Run these commands from your repository. The example assumes the local branch main exists and fix/login is a new name. Pick the branch and base for your task.

git worktree add -b fix/login ../app-fix-login main

git -C ../app-fix-login status --short --branch
git -C ../app-fix-login rev-parse HEAD
git worktree list

Give the agent or teammate the resolved directory, branch and starting commit. -b asks Git to create a branch. ../app-fix-login places its working files alongside the original directory. main supplies the starting point. An explicit base also makes the handoff reviewable. A second engineer can tell whether the task started from the local default branch, a reviewed integration branch or a specific commit.

Working files in a linked checkout come from the commit Git checks out. An uncommitted edit or an untracked local configuration file from your original directory will need separate handling. Check what the application reads from Git, the shell environment and local services before assuming the new directory is ready to run. For the decision about when a second checkout is worth using, see Git Worktree vs Branch.

Choose the branch you need

The command shape depends on whether you want to create a branch, open one that already exists, or inspect a commit temporarily. The Git worktree manual documents all three forms in its command reference.

Task Command Check before starting
Start a new fix from a reviewed base git worktree add -b fix/login ../app-fix-login main The base contains the intended changes and the new branch name is unused.
Open an existing local review branch git worktree add ../app-review review/login The branch exists locally and no other worktree owns its checkout.
Inspect a commit without moving a branch git worktree add --detach ../app-audit main You can discard the checkout after inspection or name a branch before handing off new commits.

If your team wants the base to match the remote branch, refresh the relevant remote-tracking reference under your normal repository procedure and use that explicit reference as the base. A local main can be older than origin/main. That difference matters when two tasks expect to build against the same schema or API contract. Use git rev-parse HEAD to make the actual starting point visible instead of relying on a shared branch name.

If Git reports that a branch is already checked out, use git worktree list to locate it. Carry on in that registered directory if it belongs to your task. Create a separate branch from your chosen commit if you want an independent edit. For a read-only inspection, use a detached checkout and treat an empty result from git branch --show-current as expected. Resolve any active ownership before deciding whether an existing checkout can be removed.

Check what Git keeps separate

A worktree gives an edit its own directory and index. Git still uses a common repository directory for the linked checkouts. In the fixture, staging fixture-notes.txt on fix/login left the main index empty. A named stash created from the fix appeared when we listed stashes from main. A repository-local configuration entry set in main was visible from the fix.

You can inspect the difference from either checkout.

git -C ../app-fix-login branch --show-current
git -C ../app-fix-login rev-parse --path-format=absolute --git-path index
git -C ../app-fix-login rev-parse --path-format=absolute --git-common-dir
git -C ../app-fix-login diff --cached --name-only

Check the command's working directory before loading tools that resolve the project from their starting path. The top-level directory shown by git rev-parse --show-toplevel should match the path in the task handoff. If it points at your original clone, fix that before editing or running an install. A terminal tab name or agent session label alone cannot identify the checkout.

The same distinction applies to reviews. The staged diff shows what that worktree plans to commit. It does not tell you which local service a browser is talking to or which database a test will change. The known repository base, the active process, and the state it touches each need a check.

Reproduce the port collision with two local checkouts

The fixture used a fresh repository, Git 2.53.0 and Python 3.14.4 on WSL. Both servers bound to IPv4 loopback. A one-line marker file let the HTTP response identify the checkout even if both servers returned a success status. The compact version below uses the same Git and Python HTTP server operations. It is for a temporary directory that contains only the fixture files.

lab="$(mktemp -d)"
git init --initial-branch=main "$lab/app"
printf 'branch=main\n' > "$lab/app/index.html"
git -C "$lab/app" add index.html
git -C "$lab/app" -c user.name="Worktree lab" \
  -c user.email="lab@example.invalid" commit -m "Mark main checkout"

git -C "$lab/app" branch review/login main
git -C "$lab/app" worktree add ../app-review review/login
printf 'branch=review/login\n' > "$lab/app-review/index.html"
printf 'Lab directory: %s\n' "$lab"

Choose two unused ports. The example uses 8000 and 8001. In each fresh terminal set lab to the directory printed above. In the first terminal, launch the original checkout and leave it running.

python3 -m http.server 8000 --bind 127.0.0.1 --directory "$lab/app"

In the second terminal, deliberately try the same port from the review checkout. You should see the bind fail if the first server already holds that address and port. Then start the review checkout on its own port.

python3 -m http.server 8000 --bind 127.0.0.1 --directory "$lab/app-review"
python3 -m http.server 8001 --bind 127.0.0.1 --directory "$lab/app-review"

From a third terminal, check the response from each port.

curl --fail --silent --show-error http://127.0.0.1:8000/index.html
curl --fail --silent --show-error http://127.0.0.1:8001/index.html

Expected output is branch=main and then branch=review/login. The automated fixture used OS-assigned ports during the successful launches to avoid relying on 8000 and 8001 being free. When it tried the original port the second time, the bind failed and a request still returned the main marker. Once both processes had distinct ports, each returned HTTP 200 and its own marker. A bare 200 on the port you hoped to use would have missed the collision.

For a real application, pick the equivalent request for your stack. Two different versions might show the same home page or health check. Choose a request whose response changes with the branch, or expose an explicit local build marker the reviewer can verify. Separate ports solve process addressing in this example. Name the database or schema, cache directory, and service credentials used by each checkout before running any workflow that changes shared state. The fixture did not exercise those resources.

When a request returns the wrong marker, compare the process launch directory with the checkout's resolved path. A second prerequisite is that your client is reaching the process you just started. Browser tabs, test runners and local proxy settings can keep pointing at the old port after the new checkout looks ready. Use a direct request to an explicit address before trusting the browser session. If you start servers through a task runner, check its startup output for the actual port. Some frameworks choose the next available port after a bind error, which can make the test client and the application disagree about the address.

Keep the checks narrow when handing off infrastructure work. A request from two ports can prove that the servers are reachable under different addresses. It cannot establish that both instances have independent service credentials or that their writes go to separate databases. For that, include a separate check of the target configuration and use a disposable test namespace before writing anything. Tie each observation to the one claim it supports.

Record enough for the next person to verify

A useful handoff identifies the task, branch, base commit and working directory. Add the local URL, the command you used to start the app, and the exact response or test result you saw. A reviewer can then reproduce the difference without guessing which of two running copies you were looking at. Keep local secrets outside the handoff.

If an agent writes that note, Antislop provides a prose checklist for replacing vague claims with source-backed details. In a worktree handoff, that means keeping the specific port and observed branch response instead of reporting that setup succeeded without evidence. Git commands and the HTTP response establish those facts.

The Claude Code worktrees guide and the Codex worktrees guide show where tool-managed worktrees fit. Use the same preflight when a tool creates the directory. Verify its current Git branch and which local service the task will hit. On the team board, record the owner and intended files as part of coordinating parallel coding agents. Once the change is ready, the GitHub merge queue guide covers the combined check before changes land.

Remove the finished checkout safely

Stop any foreground servers started from that directory with Ctrl+C. Check tracked changes and untracked files from the checkout you plan to remove. Make sure valuable edits are committed or accounted for, and that any review or integration has a reachable branch to follow. Run the ordinary removal command from another worktree when the target is clean and no task still needs it.

git -C ../app-review status --short --branch
git worktree list
git worktree remove ../app-review

Our fixture checked removal twice. A dirty worktree with an untracked file was preserved after the ordinary remove command refused it. The clean checkout was removed, and review/login was still listed as a branch. Decide whether to retain or delete the branch on its own merits after checking the revision another reviewer needs. The next task can then start from a known commit and a free local port, with its evidence attached to the handoff.

After removing the checkout, check which local Git branches are safe to prune. Confirm the branch tip and account for squash merges before deleting a local name.

Frequently asked questions

How do you add a Git worktree for an existing branch?

From your repository, run `git worktree add ../app-review review/login` with a new directory path and the existing branch name. Confirm that the branch has no other registered checkout, then use `git -C ../app-review status --short --branch` to check the result. The original checkout remains on its own branch.

Why does Git say the branch is already checked out?

Git normally keeps a branch checked out in only one worktree at a time. Run `git worktree list` to find the registered directory. Use that directory for the same task, or create a new branch from the required commit if another person or agent needs an independent edit.

Does a Git worktree share my staged or uncommitted changes?

Each linked worktree has its own working files and index, so staging an edit in one checkout leaves the other checkout's staging list alone. The checkouts share the repository's history, and stash entries and repository configuration are ordinarily shared. Verify these separately when two tasks run side by side.

Can two Git worktrees run an app on the same port?

Two ordinary local servers cannot bind the same TCP address and port at once. Give each checkout its own port and verify the response from each URL contains a branch-specific marker or other expected behavior. Check database and cache configuration separately when both app instances use local services.

Does removing a Git worktree delete the branch?

Removing a worktree unregisters its checkout and removes the directory when the operation succeeds. The Git branch remains. Review dirty files first and stop processes using the worktree before removing it. Decide whether to delete the branch separately after checking that other people can still reach the needed commits.