Add ability to create a worktree based on a remote branch
Open 💬 6 comments Opened Apr 22, 2026 by ustun
What variant of Codex are you using?
Codex App
What feature would you like to see?
When reviewing Github PRs, here is my workflow right now:
1/ Track the remote branch locally.
2/ Create a new worktree in Codex to review the branch.
Ideally, I should be able to skip step 1 and specify a remote branch name, where Codex switched to a worktree tracking that branch.
Additional information
_No response_
6 Comments
In the Codex app, the branch dropdown also includes remote branches that I haven't checked out on my local machine yet.
If I try to create a worktree without checking out the branch first, it fails. Once checked out, it works fine.
Codex App Bug Report ID: 019df174-ff6f-79b0-8d59-731f9771b103
I wish it worked for me, but the Codex MacOS app doesn't automatically fetch remote branches which makes it pretty useless for my work especially because there's no programmatic way to switch to another branch in the UI once a conversation has started.
I also want to eliminate the step with opening terminal and doing
git fetch origin mainbefore starting new session with worktree from that branch.I reproduced a related stale-base case when creating a new Codex-managed worktree from a local default branch.
Environment
mainThe reproduction used a temporary local bare repository, so network latency, remote authentication, and an external Git hosting service were not involved.
Reproduction steps
I reproduced this using only a temporary local bare Git repository. No external Git hosting service or network access was required.
1. Create a source checkout at commit A
2. Advance the actual remote to commit B from another checkout
At this point, do not run
git fetchorgit pullin the source checkout.Verify the intentionally stale state:
The first two commands should return commit A, while
ls-remoteshould return commit B.3. Create a Codex worktree
$SOURCEdirectory as a project in Codex App.mainas the starting branch.git fetch,git pull, orgit reset.4. Observe the result
The new Codex worktree was created at commit A, even though the actual remote
mainhad already advanced to commit B.Repository state before worktree creation
The source checkout was intentionally left stale.
The source checkout was clean:
The source checkout's latest commit was:
Result
Codex created the new managed worktree at the stale local commit:
The worktree was detached:
The worktree status was:
The worktree's latest commit was:
The worktree list showed:
The final state was therefore:
| Ref or worktree | Commit |
| --- | --- |
| Local
main|3ddf2585df630c0289062b2a6ea2f428cf9cb549|| Cached
origin/main|3ddf2585df630c0289062b2a6ea2f428cf9cb549|| Actual remote
main|1137083843a3ab65cfa4cf9d1224d9f49190b91e|| New Codex worktree |
3ddf2585df630c0289062b2a6ea2f428cf9cb549|In other words:
The
origin/mainreflog also showed no fetch or update during worktree creation:This demonstrates that Codex created the worktree from the stale local branch HEAD without first refreshing the corresponding remote-tracking branch.
Expected behavior
This appears closely related to creating worktrees directly from remote branches. Both cases require Codex to refresh and resolve the intended base commit before creating the worktree.
I suggest avoiding an automatic
git pull, because that would mutate the user's local branch and could introduce merge or rebase behavior.A safer flow would be:
When the local branch is only behind its upstream, Codex could create the worktree from the fetched upstream OID without updating
refs/heads/main.For ahead, diverged, no-upstream, dirty, or fetch-failure states, Codex should ask the user or report the state instead of silently merging, rebasing, or falling back to a stale commit.
A key regression assertion could be:
Would the maintainers prefer this stale-local-default-branch case to remain under #18961, or should it be tracked as a separate pre-worktree base-refresh issue?
Also, is the relevant base-resolution logic part of the open-source App Server or Git utility layer? I would be interested in contributing tests and an implementation if an external PR is invited.
I am seeing the same stale-starting-branch failure mode in Codex App worktree mode, especially when starting from a clean local
developbranch that is behindorigin/develop.Environment where this is still relevant:
developPractical failure mode:
developat commit A.origin/develophas advanced to commit B.developand the cachedrefs/remotes/origin/developstill point at A.develop.developat B.The important safety point is that fixing this should not require
git pullor moving the user's local branch. Since Codex creates a detached worktree, it can safely resolve a fresh immutable base OID first:git pull, or moverefs/heads/develop/refs/heads/main;A useful regression assertion would be:
A
SessionStarthook is not an equivalent workaround: it fires after the worktree already exists, and at that point the hook cannot reliably know which branch/upstream was selected to create the detached worktree. This seems best handled in Codex's pre-worktree base resolution step.I am seeing the same stale-base problem in the Codex macOS app with a repository whose default development branch is
staging.When starting a new worktree chat, Codex defaults the branch picker to
staging. The intended base is the latestorigin/staging, but the localstagingref may be behind. This can create a new detached worktree from stale code unless I leave Codex and manually fetch or pull first.The ideal behavior would be either:
origin/staging; orThis should not move, merge, rebase, or otherwise modify the local
stagingbranch. If the local branch is ahead, diverged, dirty, lacks an upstream, or the fetch fails, Codex should show that state and ask what to do rather than silently using a stale commit.In short: new worktree chats should be able to start from the current upstream commit while leaving the main local checkout untouched.