Codex Worktree Detached HEAD: Create a Branch
Prepared and checked using AI. Sources are linked in the article.
To create a branch for a Codex worktree in detached HEAD state, open a terminal inside that worktree and run git switch -c codex/fix-parser. Git creates the branch at the current commit and switches the worktree to it. Existing commits at that detached HEAD become reachable through the new branch, so you can review, push, or merge them by name.
The paths, branch names, remote name, commit message, and commit hash below are examples; use values from your repository.
Confirm which worktree you are changing
The Codex app supports separate worktrees for agents working on the same repository. The branch command belongs in the worktree containing the changes you want to keep. If that directory is ../repo-codex-task, enter it before running any of the commands below:
cd ../repo-codex-task
git status --short --branch
git worktree list
git status --short --branch shows both the branch state and changed files. git worktree list shows each worktree’s path and marks one without a branch as (detached HEAD). Match its path to your current directory. Another worktree can have a normal branch while the Codex worktree is detached; creating a branch in the wrong directory will leave the Codex commits where they were.
Detached HEAD means this worktree’s HEAD points directly to a commit rather than to a branch name. It does not mean the files or commits have disappeared. Git allows commits in this state, but if you later switch away without giving those commits a reference, recovering them becomes an extra step. Create the branch while the desired commit is still checked out.
Create the branch at the current detached HEAD
Choose a name that describes the task and is not already in use. Then, from the detached worktree, run:
git switch -c codex/fix-parser
git status --short --branch
git worktree list
The second command should now show the branch name. The worktree listing should show that name in brackets beside the same path. You do not need to create another worktree or copy files: this changes which branch the existing worktree has checked out.
Run this after Codex has made commits or before its first commit. In the first case, the new branch starts at the latest detached commit and retains its history. In the second, it gives subsequent commits a named destination. git switch -c does not stage or commit edits still in the working tree; inspect git status after switching so you know what remains unfinished.
If there are uncommitted edits
Branching and committing answer different questions. A branch names the commit at HEAD; uncommitted edits still need review and a commit before another worktree can integrate them as commits. Git can switch branches with local edits when doing so will preserve them, but it stops if the operation would lose those edits. Creating a branch at the current detached HEAD is the direct route because it does not ask Git to move to a different starting commit.
Review the changed files before staging. For a focused change, stage selected paths with git add, then commit the staged content with git commit:
git add src/parser.ts test/parser.test.ts
git diff --cached
git commit -m "Fix parser error handling"
git status --short --branch
git diff --cached lets you inspect what the commit would contain. Avoid staging every file merely because an agent changed it. Generated files, temporary output, or unrelated edits deserve a separate decision. If the worktree has no edits and already contains Codex commits, there is nothing additional to commit just to create the branch.
If the branch name is taken
If git switch -c codex/fix-parser says the branch already exists, first inspect the existing branches and worktrees. git branch -vv can show the linked worktree path for a checked-out branch:
git branch -vv
git worktree list
If the name belongs to another task, choose a distinct name, such as codex/fix-parser-followup. If it is the branch you meant to use, check its current commit and worktree before switching. Git normally refuses to check out the same branch in two worktrees, because both would then try to advance one branch. The practical choices are to continue in the worktree that already owns it or give this Codex worktree its own branch. See safe fixes for a branch already checked out for that case.
Do not use git switch -C as a routine workaround: that form can reset an existing branch to a new starting point. Likewise, forcing a second worktree onto the same branch removes a useful Git safeguard. A fresh task name is usually easier to reason about than changing an existing branch reference.
Review and integrate the named branch
After creating the branch, compare it with the branch you intend to merge into. git log main..codex/fix-parser selects commits reachable from the task branch but not from main, while git diff main...codex/fix-parser compares the task tip with their common ancestor:
git log --oneline main..codex/fix-parser
git diff --stat main...codex/fix-parser
git diff main...codex/fix-parser
The log answers which commits would be introduced; the diff shows the resulting code changes. Review both, especially if the agent made several commits or the base branch moved during the task. A named branch makes this comparison repeatable from another worktree without relying on a remembered detached commit ID.
For local integration, move to the worktree where main is checked out and make sure it has no unfinished edits. Then merge the task branch:
git merge --ff-only codex/fix-parser
--ff-only advances main only when the merge can be a fast-forward; otherwise Git refuses. That refusal can be useful when main has acquired commits since the Codex task began. Review the changed history and choose your repository’s normal merge or rebase process rather than assuming the branches still line up. For a fuller integration path, see merging parallel agent branches.
If the branch needs to be shared or submitted for review, push it from either worktree:
git push -u origin codex/fix-parser
-u sets the pushed branch as the local branch’s upstream. Pushing publishes the commits on the branch; uncommitted working-tree edits are still local.
After integration, return to the Codex worktree and inspect tracked, untracked, and ignored files before removing it:
git status --short --ignored --untracked-files=all
The !! entries are ignored files; --untracked-files=all shows individual files inside ignored directories. Preserve anything you need, including ignored files, before removal. Git recursively deletes the worktree directory during git worktree remove, including ignored contents that ordinary git status omits. Then remove the linked worktree from another worktree. Removing the worktree and deleting its branch are separate actions. Keep the branch until you are satisfied that its commits have been integrated or preserved remotely. The worktree cleanup guide covers that final check.
If you already switched away from the detached commit
If you left detached HEAD before creating a branch, look for the commit in the reflog of the worktree where the work happened. git reflog records recent changes to HEAD, including branch switches:
git reflog HEAD
Find the commit you want to preserve, verify its contents, and create a branch at its hash:
git branch codex/recovered 1234abc
This creates a reference without changing the current checkout. Then open an available worktree on that branch, or merge it by name. Do this promptly: reflog entries expire, and an unreferenced commit is not a durable place to store work. If the original worktree has already been removed, recovery is less straightforward because its local HEAD history may no longer be available.
Prevent the problem on the next task
When creating a worktree yourself, name its branch at creation time:
git worktree add -b codex/fix-parser ../repo-codex-task main
-b creates the branch at main and checks it out in the new worktree. By comparison, git worktree add --detach deliberately creates a worktree without a branch. If a tool has already given you a detached worktree, the earlier git switch -c command handles it in place; creating a second worktree is unnecessary.
Where Parallel Code fits
Parallel Code is our free, open-source desktop app for macOS and Linux. It runs Codex CLI and other AI coding agents in parallel, each isolated in its own Git worktree, and provides a diff-first review surface. You can still use the Git commands above in an agent’s worktree when you need a named branch for integration.
Frequently asked questions
Will creating a branch keep commits Codex already made in detached HEAD?
Yes. Run git switch -c while that detached worktree is still at the desired commit. The new branch starts there, so the commit and its ancestors are reachable through the branch name.
Does git switch -c commit Codex’s uncommitted changes?
No. It creates and checks out a branch; edits in the index or working tree remain separate until you stage and commit them. Check git status --short --branch afterward and review what still needs to be saved.
Can I create the branch from my main worktree instead?
You can create a branch at a known commit hash from another worktree, but that will not attach the Codex worktree to it. To turn the current detached checkout into a branch in one step, run git switch -c inside the Codex worktree.
Why does Git say the branch is checked out in another worktree?
A branch is normally checked out in only one worktree at a time. Find its owner with git worktree list, then either work there or create a distinct branch for the Codex worktree. Avoid forcing both worktrees onto the same branch.