·

Fix Git config.lock Errors in Parallel Worktrees

Prepared and checked using AI. Sources are linked in the article.

git-worktrees multi-agent workflow ai-coding

If git worktree add fails with a config.lock error while several agents start together, their Git operations may be competing to write the same repository configuration. Let the other creation attempts finish, inspect which branches and worktrees exist, then create only the missing worktrees one at a time. A reported parallel agent failure includes both could not lock config file .git/config and unable to write upstream branch configuration. In Git’s normal worktree add -b sequence, that tracking failure can leave the new branch behind without creating its worktree.

Why separate worktrees can share a config lock

A linked worktree has its own working directory, HEAD, and index, but the repository config file is shared by default. That distinction matters during setup. Git may need to write branch tracking configuration even though each agent will edit files in a separate worktree. Concurrent commands can therefore meet at the same config.lock file.

The second error is a useful clue. Git creates the branch before writing its tracking settings, and emits unable to write upstream branch configuration if that write fails. For git worktree add -b, Git stops when branch creation returns an error, before calling add_worktree. The surviving state from this native failure is therefore a branch without its new worktree. If you also find a worktree, look for another explanation, such as a successful retry or a later hook or setup failure. Inspect the state before deciding what to run next.

A lock error can also have another cause: a Git process may still hold the lock, or a lock file may remain after a process ended. Git’s lock error handling distinguishes a possible active process from a potentially stale lock. Give concurrent setup commands time to finish before considering any lock file stale. Removing a lock while its owner is writing can interfere with that write.

Inspect the repository before retrying

Run the following from a worktree connected to the affected repository. For example, suppose the intended new path is ../agent-b and its branch is agent/b:

git rev-parse --git-common-dir
git rev-parse --git-path config
git worktree list --porcelain
git branch -vv

Git’s path resolution commands identify the common Git directory and resolve Git paths. Use the reported lock path when investigating; do not assume a linked worktree’s .git is a directory containing the shared config. git worktree list --porcelain gives a stable format for identifying registered paths and branches. git branch -vv helps you see whether the intended branch exists and whether tracking information appears.

If the intended worktree is listed, inspect its files before launching an agent there:

git -C ../agent-b status --short

git status --short shows tracked changes and untracked files. A worktree listed after a tracking error may have come from a successful retry or from setup that failed at a later step. If an agent or setup hook wrote files, a fresh creation attempt could put that work at risk. Keep the existing worktree and resolve any missing setup separately.

Use the results to choose the next command:

State after the failureNext step
Path and branch appear in git worktree listKeep the worktree. Check its status and any required tracking configuration.
Branch exists, but the intended path is not registeredConfirm the path is free, then add a worktree using the existing branch.
Neither branch nor worktree existsCreate the worktree with a new branch after other creation commands finish.
Path exists on disk but is not listedInspect its contents and .git entry before changing anything.
List marks an entry prunableVerify the worktree really is gone before pruning its metadata.

The path and branch checks are separate. A tracking write failure during native worktree add -b can leave the branch without the worktree. Repeating the original -b command when that branch already exists will fail for a different reason.

Retry only the missing creation

If the branch exists but no worktree uses it, create the worktree from that branch:

git worktree add ../agent-b agent/b

If neither exists, create the branch as part of the worktree add. Here main is an example starting point; replace it with the commit or branch appropriate for the task:

git worktree add -b agent/b ../agent-b main

Wait for this command to finish and check git worktree list --porcelain before starting another creation. Once setup is complete, the agents can work in their separate directories concurrently. Serializing creation avoids competing config writes at the point where they matter; it does not require serializing the agents’ coding work.

If several creation attempts failed, handle each intended path and branch independently. Avoid a loop that blindly repeats the same git worktree add -b ... command: the first attempt may have created the branch, or a later attempt may have succeeded while the orchestration layer still reports an earlier error. An automated retry should respond only to the lock error, wait briefly, recheck registered worktrees and branches, and stop on another error. Give each agent a distinct path and branch name.

A branch already checked out by another worktree needs a different decision. Git normally refuses to add a second worktree on that branch; see the branch already checked out fixes rather than using --force to treat it as a config lock problem.

Repair tracking information after creation

A tracking write failure can leave a branch without an upstream and without a worktree. First create its worktree with the existing-branch command above, then inspect git branch -vv. The same tracking check applies if a worktree was already present because a retry succeeded or a later setup step failed.

If that branch was meant to track a remote branch, set the intended upstream after the competing writes have stopped. For example:

git branch --set-upstream-to=origin/main agent/b

--set-upstream-to changes a branch’s tracking branch. Use origin/main only if it is the upstream you actually want; the commit used to start an agent branch does not automatically determine its ongoing tracking policy. If the agent branch is local only, it does not need an upstream merely because Git printed a tracking error. Recheck git branch -vv after setting one.

When waiting does not clear the error

If config writes keep colliding, create worktrees through a single queue in the launcher. For a manual session, run one git worktree add at a time and start agents after their directories exist. This addresses contention without changing repository settings.

Current Git defines core.configLockTimeout as the time it retries acquiring a config file lock for writing, with a documented default of 1000 milliseconds. If your installed Git supports that key and short overlaps are routine, you can set a longer timeout after setup has stopped competing for the lock:

git config --local core.configLockTimeout 5000

The value is in milliseconds. This gives a config writer more time to wait; it does not fix a lock left behind by a stopped process, a path collision, or a command that failed for another reason. Check the installed Git’s git help --config output before relying on the key, especially across machines with different Git versions.

If no Git process is still working and the same lock file remains, investigate that specific file and the command that last wrote config. Do not make rm -f .git/config.lock part of an automatic retry: the literal .git path may not identify the shared directory from a linked worktree, and an active writer may still own the lock. Once the lock condition is resolved, inspect worktree and branch state again before rerunning creation.

A separate cleanup case is a missing worktree whose registration remains. Preview what Git would remove with git worktree prune --dry-run, then prune only when the missing directories are genuinely gone. If a worktree was moved, use git worktree repair to reconnect it instead. The worktree cleanup guide covers the broader cleanup workflow.

Where Parallel Code fits

Parallel Code is our free, open-source (MIT) desktop app for macOS and Linux. It runs several AI coding agents in parallel, each isolated in its own git worktree, and gives you a diff-first review surface. If you manage agent worktrees yourself, the inspection and retry steps above also apply to your setup.

Frequently asked questions

Should I delete .git/config.lock as soon as I see the error?

No. First let any concurrent Git commands finish and check whether another process is writing config. Only investigate removal if the lock persists after its owner has stopped, and resolve the actual lock path for that repository.

Can a failed git worktree add leave a usable worktree?

The native tracking write failure during worktree add -b can leave a branch, but stops before Git creates its worktree. If a worktree is listed, a retry may have succeeded or a later setup step may have failed. Check its status before changing it.

Does core.configLockTimeout replace serial creation?

No. A longer timeout may help a config write wait through a brief overlap, but serial creation gives each attempt a clear result to inspect. It also makes partial setup easier to handle before the agents begin work.

Should I use --force to retry the same branch?

A config lock and a branch already checked out elsewhere are different failures. Inspect registered worktrees first; if another agent uses the branch, choose a distinct branch for the new task. Use --force only when you understand the safeguard it overrides.