{"slug": "how-to-fork-a-claude-code-or-codex-session-to-try-another-approach", "title": "How to fork a Claude Code or Codex session to try another approach", "summary": "A developer documented how to fork Claude Code and Codex agent sessions so an alternative approach can be tried without polluting or losing the original conversation context. Claude Code offers /branch and the --fork-session flag, while Codex provides /fork, codex fork, and a double-Esc prompt picker, and the writeup recommends giving each fork its own git worktree to avoid two agents editing the same files.", "body_md": "The agent has a plan, and it's halfway through. You want to see what the other approach looks like, the one you talked it out of an hour ago. If you ask in the same session, you pollute the context it has built up. If you start a new session, you lose that context.\n\nA fork gives you both. It copies the conversation so far into a new session, and the original stays exactly where it was:\n\n`/branch`. You're moved into the copy, and the original can be resumed later.`claude --resume <session-id> --fork-session`.`/fork`. From the shell: `codex fork <session-id>`, or `codex fork --last`.\nFork when you want to keep the current thread intact:\n\nIf you just want to undo the last few steps, you don't need a fork. Claude Code's `/rewind` (also `/checkpoint`) restores the conversation, the code, or both to an earlier message.\n\nInside a running session, type:\n\n```\n/branch try-event-queue\n```\n\nClaude Code copies the conversation into a new session and switches you into it. The name is optional. The fork's title ends in \"(Branch)\", so it's easy to tell apart in `/resume`. The confirmation message includes the command to get back to the original, `claude -r <original-session-id>`.\n\nFrom the shell, add `--fork-session` to a resume:\n\n```\n# Fork the most recent session in this directory\nclaude --continue --fork-session\n\n# Fork a specific session\nclaude --resume <session-id> --fork-session\n```\n\nWithout `--fork-session`, both commands reopen the original session and keep adding to it.\n\nInside a running session, type `/fork`. From the shell:\n\n```\n# Pick a session to fork from a list\ncodex fork\n\n# Fork the most recent session in this directory\ncodex fork --last\n\n# Fork a specific session\ncodex fork <session-id>\n```\n\nLike `codex resume`, the picker and `--last` only look at sessions from the current directory. Add `--all` to see every directory.\n\nCodex has two more ways to branch. Press `Esc` twice to step back through your earlier prompts, pick one, and press `Enter`. Codex forks the conversation just before that prompt and puts the prompt back in the composer for you to edit. The original session keeps the full history. For a quick tangent, `/side` starts a side conversation in a temporary fork.\n\nNo. A fork copies the conversation. Both sessions still work in the same directory, on the same files. If the original agent is editing `src/queue.ts` while the fork rewrites it, you get a mess that neither of them planned.\n\nGive the fork its own git worktree. A new worktree starts from your last commit, not from the files on disk, so uncommitted edits and new files don't come along. If the agent has been working for a while, commit first:\n\n```\ngit add -A && git commit -m \"wip: before fork\"\n```\n\nYou can squash or amend that commit later. Without it, the fork remembers changes that its worktree doesn't have. If you've used `claude --worktree` in this repo before, add `.claude/worktrees/` to `.gitignore` first, or `git add -A` will pick up those worktree folders too.\n\nThen fork into a worktree. Both CLIs can create one for you:\n\n```\n# Claude Code: fork into a new worktree under .claude/worktrees/try-event-queue\nclaude --worktree try-event-queue --resume <session-id> --fork-session\n\n# Codex: fork into a new worktree under ~/.codex/worktrees\ncodex fork --worktree <session-id>\n```\n\nClaude Code puts its worktree on a new branch named `worktree-<name>`. Codex needs an explicit session ID here, since `--last` doesn't work with `--worktree`, and its worktree starts on a detached HEAD, so create a branch there (`git switch -c try-event-queue`) before you commit anything you want to keep.\n\nOr create the worktree yourself and fork from inside it:\n\n```\ngit worktree add ../myapp-event-queue -b try-event-queue\ncd ../myapp-event-queue\nclaude --resume <session-id> --fork-session\n```\n\nThe fork still has the whole conversation, even though it now runs in a different directory. When one approach wins, merge its branch and remove the other worktree by path, for example `git worktree remove ../myapp-event-queue`. `git worktree list` shows the paths, including the ones Claude Code and Codex created.\n\nForks look alike in a session list, since they start from the same conversation. Name them when you create them:\n\n`/branch <name>`, or `/rename` inside any session.\nA name that says what the fork is trying, such as \"event queue\" or \"keep polling\", makes the right session easy to find in `claude --resume` or `codex resume` a day later.\n\nYes. In [PonyMux](https://ponymux.com), select a Terminal that's running Claude Code or Codex, open the Inspector, and click **Fork Session** in the Session section. The same action is in each session's ⋯ menu in the Sessions panel.\n\n*The Inspector's Session section for a Claude Code session. Fork Session is right above Handoff.*\n\nPonyMux asks where to put the fork:\n\nIt runs `claude --resume … --fork-session` or `codex fork …` for you, in the session's working directory. A Terminal that holds a fork shows an **Origin** section in the Inspector, with **Go to Source Session** to jump back to the conversation it came from.\n\nPonyMux doesn't create a worktree for the fork, and it always starts the fork in the original session's working directory. If the two sessions will edit files, commit first as described above, create the worktree yourself, open a Terminal in it, and run `claude --resume <session-id> --fork-session` there, or use `codex fork --worktree`. A session needs at least one saved exchange before it can be forked.\n\nYou also don't need to remember which session was which. Each PonyMux Terminal keeps a timeline of every Claude Code and Codex session that has run in it. Open the Sessions panel to see them, with their titles, recaps, and when each was last used. Click **Resume** on any row to go back to that exact session, not just the most recent one.\n\n*The Sessions panel lists every conversation that has run in this Terminal. Resume reopens the one you pick.*\n\nResume needs the agent's transcript to still be on disk. Claude Code deletes transcripts after 30 days by default; see [Claude Code conversation history](https://ponymux.com/guides/claude-code-conversation-history) for how to keep them longer.\n\nFor the details of Resume, Fork, and Handoff, see the [Sessions docs](https://ponymux.com/docs/sessions). To move a task to the other agent instead of copying it, see [How to hand off a task from Claude Code to Codex](https://ponymux.com/guides/hand-off-claude-code-to-codex).\n\n*Originally published on [ponymux.com](https://ponymux.com/guides/fork-claude-code-codex-session), where the demos are interactive.*", "url": "https://wpnews.pro/news/how-to-fork-a-claude-code-or-codex-session-to-try-another-approach", "canonical_source": "https://dev.to/heylittlepan/how-to-fork-a-claude-code-or-codex-session-to-try-another-approach-33e3", "published_at": "2026-09-29 12:08:26+00:00", "updated_at": "2026-09-29 12:16:52.308522+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools"], "entities": ["Claude Code", "Codex", "Anthropic", "OpenAI", "git"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/how-to-fork-a-claude-code-or-codex-session-to-try-another-approach", "markdown": "https://wpnews.pro/news/how-to-fork-a-claude-code-or-codex-session-to-try-another-approach.md", "text": "https://wpnews.pro/news/how-to-fork-a-claude-code-or-codex-session-to-try-another-approach.txt", "jsonld": "https://wpnews.pro/news/how-to-fork-a-claude-code-or-codex-session-to-try-another-approach.jsonld"}}