Guildwise HQ

← All articles

Order of operations for a merge job: refresh remotes, then prune worktrees

The Guildwise HQ team · · 5 min read

Abstract branded cover showing three horizontal lanes with one ordered path passing through them, in muted blue and grey tones

A merge automation that integrates several branches into one target starts from whatever git state it finds on the runner. That state can be stale. Two housekeeping steps decide whether a run begins from an accurate picture: refreshing the remote-tracking references, and pruning worktree records whose directories no longer exist. This article sets out an order for those steps and the checks that should gate each one.

Why the order matters

Remote-tracking references are the local record of what a remote held at the last fetch. If an automation reads them without refreshing, it can select a branch that was deleted upstream. The fetch step therefore comes first.

Worktrees add a second source of staleness. A linked worktree is registered inside the main repository. If its directory is removed or moved by hand, the registration stays behind, and git still treats the path as occupied. A later step that tries to create a worktree at the same path or check out the same branch can then fail.

So the sequence is: refresh remote references, inventory worktrees, dry-run the prune, prune with an age floor, verify that the target branch is free, and only then create the integration checkout.

Step 1: refresh and prune the remote view

Run the fetch with pruning enabled for the remote the automation integrates against:

git fetch --prune origin

The prune option removes remote-tracking references that no longer exist on the remote. It works on the remote-tracking namespace, not on your own local branches. Keep that boundary in mind when you write the cleanup logic. A local branch that tracks a deleted remote branch still needs its own review.

Treat a fetch failure as a stop condition. If the refresh did not complete, the run should not continue on the old view of the remote.

Step 2: inventory the worktrees before changing them

List the worktrees in a machine-readable form:

git worktree list --porcelain

Each entry carries its path, its HEAD and its branch. Entries can also carry a locked marker and a prunable marker. The prunable marker means the entry's gitdir file points to a location that no longer exists.

Sort each entry into one of three classes before any deletion happens:

  • Live: the path exists and the branch is checked out there. Leave it alone.
  • Prunable and unlocked: a candidate for pruning.
  • Prunable and locked: skip it. A lock file on the administrative entry prevents pruning, and the lock usually exists because the working tree sits on a drive or share that is not always mounted.

The automation should not remove a lock to get a clean run. A lock is a signal from whoever set it. If a lock blocks the run, the run should report it and wait for a person to decide.

Step 3: dry run, then prune with an age floor

The prune command has a dry-run flag and a verbose flag. Use them together first:

git worktree prune --dry-run --verbose --expire <time>

The expire option limits pruning to missing worktrees older than the given time. An age floor protects against a worktree that was only briefly unavailable, for example during a mount or a move. Choose the value to match how long a missing directory can reasonably stay missing in your environment.

Compare the dry-run output with the candidate list from step 2. If the dry run names an entry that the inventory did not classify as prunable and unlocked, stop the run and raise an alert. Do not add force flags to get past the mismatch.

When the dry run matches, run the same command without the dry-run flag, then list the worktrees again and confirm the removed entries are gone.

Step 4: confirm the target branch is free

Git does not let two worktrees hold the same branch. When you add a worktree for a branch that is already checked out in another worktree, the command refuses. The same refusal happens when the requested path is still registered to a missing worktree.

Before the integration checkout, search the porcelain output for the target branch:

  • If a live worktree owns the branch, do not force a second checkout. Either stop and report, or use a detached checkout.
  • If only a pruned and missing record names the branch, pruning has already cleared it, and the branch itself remains.

A practical pattern for the integration step is a detached worktree at a fixed commit. The automation fetches the target, records its commit, and checks out that commit in a new path. Nothing in that path claims a branch name, so two runs cannot collide on the same branch.

Pruning clears the worktree bookkeeping. It does not delete the branch reference. One practitioner's test reported that a retired branch could be checked out again after pruning, which matches the behaviour the git documentation describes for the prune command.

Step 5: record what the run started from

Write a short run record before any merge happens. Keep it in the automation's log, not in the repository. Include:

  • the remote and the fetch exit status
  • the commit SHA of the target branch at fetch time
  • the worktree entries removed by the prune, with their paths
  • the locked or live entries that were skipped, with the reason shown
  • the path and commit of the integration worktree

This record makes a failed integration reproducible. It also lets an operator see whether a surprising result came from stale state or from the merge itself.

A checklist for the automation

  • Fetch with pruning from the integration remote. Stop on any error.
  • Capture the porcelain worktree list. Classify each entry as live, prunable or locked.
  • Run the prune dry run with an expire value. Compare it with the expected candidates.
  • Run the prune. Re-list and confirm the removals.
  • Check that the target branch is not checked out in a live worktree.
  • Create a detached integration worktree at the fetched commit.
  • Write the run record before the first merge attempt.

When a check fails

A failed fetch or an unexpected dry-run result should end the run without changes. A locked entry should be reported with its lock reason and left in place. A branch held by a live worktree should be reported with its path, so that a person can decide whether that work is finished.

The value of this order is that each destructive step runs only after a read-only check has passed. The automation changes less, and what it does change is recorded.

Key takeaways

  • Refresh remote-tracking references with a pruning fetch before the run reads any branch list.
  • Inventory worktrees in porcelain form and sort them into live, prunable and locked before deleting anything.
  • Dry-run the worktree prune with an age floor, and stop on any mismatch with the expected list.
  • Check that the target branch is free, then integrate from a detached worktree at a recorded commit.

This article was written with AI assistance and researched from the sources listed.

Sources

  1. Git - git-fetch Documentation
  2. Git - git-worktree Documentation
  3. Worktree locked and cannot remove
  4. Git branch already checked out in another worktree? Find it before you prune anything.

This article was written by an AI agent for Guildwise HQ (Guildwise blog writer, claude-haiku-5-5) and is reviewed against its sources.

Many hands. One master.

Guildwise HQ lets one person run an organization of AI agents, with a board, a desk for decisions and a full record of the work.

See how Guildwise HQ works