1# Worktrees1# Worktrees
2 2
3In the Codex app, worktrees let Codex run multiple independent tasks in the same project without interfering with each other. For Git repositories, [automations](https://developers.openai.com/codex/app/automations) run on dedicated background worktrees so they don't conflict with your ongoing work. In non-version-controlled projects, automations run directly in the project directory. You can also start threads on a worktree manually, and use Handoff to move a thread between Local and Worktree.3In the ChatGPT desktop app, worktrees let Codex run multiple independent tasks in the same project without interfering with each other. For Git repositories, [scheduled tasks](https://learn.chatgpt.com/docs/automations) can run on dedicated background worktrees so they don't conflict with your ongoing work. In non-version-controlled projects, scheduled tasks run directly in the project directory. You can also start tasks in a worktree manually and use Handoff to move a task between Local and Worktree.
4
5Worktrees are available only in Codex in the ChatGPT desktop app. Select
6 **Codex** before you start a task in a worktree.
4 7
5## What's a worktree8## What's a worktree
6 9
8 11
9## Terminology12## Terminology
10 13
11- **Local checkout**: The repository that you created. Sometimes just referred to as **Local** in the Codex app.14- **Local checkout**: The repository that you created. Sometimes just referred to as **Local** in the ChatGPT desktop app.
12- **Worktree**: A [Git worktree](https://git-scm.com/docs/git-worktree) that was created from your local checkout in the Codex app.15- **Worktree**: A [Git worktree](https://git-scm.com/docs/git-worktree) that was created from your local checkout in the ChatGPT desktop app.
13- **Handoff**: The flow that moves a thread between Local and Worktree. Codex handles the Git operations required to move your work safely between them.16- **Handoff**: The flow that moves a task between Local and Worktree. Codex handles the Git operations required to move your work safely between them.
14 17
15## Why use a worktree18## Why use a worktree
16 19
171. Work in parallel with Codex without disturbing your current Local setup.201. Work in parallel with Codex without disturbing your current Local setup.
182. Queue up background work while you stay focused on the foreground.212. Queue up background work while you stay focused on the foreground.
193. Move a thread into Local later when you're ready to inspect, test, or collaborate more directly.223. Move a task into Local later when you're ready to inspect, test, or collaborate more directly.
20 23
21## Getting started24## Getting started
22 25
26 29
271. Select "Worktree"301. Select "Worktree"
28 31
29 In the new thread view, select **Worktree** under the composer.32 In the new task view, select **Worktree** under the composer.
30 Optionally, choose a [local environment](https://developers.openai.com/codex/app/local-environments) to run setup scripts for the worktree.33 Optionally, choose a [local environment](https://learn.chatgpt.com/docs/environments/local-environment) to run setup scripts for the worktree.
31 34
322. Select the starting branch352. Select the starting branch
33 36
35 38
363. Submit your prompt393. Submit your prompt
37 40
38 Submit your task and Codex will create a Git worktree based on the branch you selected. By default, Codex works in a ["detached HEAD"](https://git-scm.com/docs/git-checkout#_detached_head).41 Submit your prompt, and Codex creates a Git worktree based on the branch you selected. By default, Codex works in a ["detached HEAD"](https://git-scm.com/docs/git-checkout#_detached_head).
39 42
404. Choose where to keep working434. Choose where to keep working
41 44
42 When you're ready, you can either keep working directly on the worktree or hand the thread off to your local checkout. Handing off to or from local will move your thread _and_ code so you can continue in the other checkout.45 When you're ready, you can either keep working directly on the worktree or hand the task off to your local checkout. Handing off to or from Local moves your task _and_ code so you can continue in the other checkout.
43 46
44</WorkflowSteps>47</WorkflowSteps>
45 48
46## Working between Local and Worktree49## Working between Local and Worktree
47 50
48Worktrees look and feel much like your local checkout. The difference is where they fit into your flow. You can think of Local as the foreground and Worktree as the background. Handoff lets you move a thread between them.51Worktrees look and feel much like your local checkout. The difference is where they fit into your flow. You can think of Local as the foreground and Worktree as the background. Handoff lets you move a task between them.
49 52
50Under the hood, Handoff handles the Git operations required to move work between two checkouts safely. This matters because **Git only allows a branch to be checked out in one place at a time**. If you check out a branch on a worktree, you **can't** check it out in your local checkout at the same time, and vice versa.53Under the hood, Handoff handles the Git operations required to move work between two checkouts safely. This matters because **Git only allows a branch to be checked out in one place at a time**. If you check out a branch on a worktree, you **can't** check it out in your local checkout at the same time, and vice versa.
51 54
52In practice, there are two common paths:55In practice, there are two common paths:
53 56
541. [Work exclusively on the worktree](#option-1-working-on-the-worktree). This path works best when you can verify changes directly on the worktree, for example because you have dependencies and tools installed using a [local environment setup script](https://developers.openai.com/codex/app/local-environments).571. [Work exclusively on the worktree](#option-1-working-on-the-worktree). This path works best when you can verify changes directly on the worktree, for example because you have dependencies and tools installed using a [local environment setup script](https://learn.chatgpt.com/docs/environments/local-environment).
552. [Hand the thread off to Local](#option-2-handing-a-thread-off-to-local). Use this when you want to bring the thread into the foreground, for example because you want to inspect changes in your usual IDE or can run only one instance of your app.582. [Hand the task off to Local](#option-2-handing-a-task-off-to-local). Use this when you want to bring the task into the foreground, for example because you want to inspect changes in your usual IDE or can run only one instance of your app.
56 59
57### Option 1: Working on the worktree60### Option 1: Working on the worktree
58 61
60 63
61<div>64<div>
62 65
63If you want to stay exclusively on the worktree with your changes, turn your worktree into a branch using the **Create branch here** button in the header of your thread.66If you want to stay exclusively on the worktree with your changes, turn your worktree into a branch using the **Create branch here** button in the task header.
64 67
65From here you can commit your changes, push your branch to your remote repository, and open a pull request on GitHub.68From here you can commit your changes, push your branch to your remote repository, and open a pull request on GitHub.
66 69
69</div>72</div>
70 73
71<CodexScreenshot74<CodexScreenshot
72 alt="Worktree thread view with branch controls and worktree details"75 alt="Worktree task view with branch controls and worktree details"
73 lightSrc="/images/codex/app/worktree-light.webp"76 lightSrc="/images/codex/app/worktree-light.webp"
74 darkSrc="/images/codex/app/worktree-dark.webp"77 darkSrc="/images/codex/app/worktree-dark.webp"
75 maxHeight="400px"78 maxHeight="400px"
80 83
81Remember, if you create a branch on a worktree, you can't check it out in any other worktree, including your local checkout.84Remember, if you create a branch on a worktree, you can't check it out in any other worktree, including your local checkout.
82 85
83### Option 2: Handing a thread off to Local86<a id="option-2-handing-a-thread-off-to-local"></a>
87<a id="option-2-handing-a-chat-off-to-local"></a>
88
89### Option 2: Handing a task off to Local
84 90
85<div class="feature-grid">91<div class="feature-grid">
86 92
87<div>93<div>
88 94
89If you want to bring a thread into the foreground, click **Hand off** in the header of your thread and move it to **Local**.95If you want to bring a task into the foreground, select **Hand off** in the task header and move it to **Local**.
90 96
91This path works well when you want to read the changes in your usual IDE window, run your existing development server, or validate the work in the same environment you already use day to day.97This path works well when you want to read the changes in your usual IDE window, run your existing development server, or validate the work in the same environment you already use day to day.
92 98
93Codex handles the Git steps required to move the thread safely between the worktree and your local checkout.99Codex handles the Git steps required to move the task safely between the worktree and your local checkout.
94 100
95Each thread keeps the same associated worktree over time. If you hand the thread back to a worktree later, Codex returns it to that same background environment so you can pick up where you left off.101Each task keeps the same associated worktree over time. If you hand the task back to a worktree later, Codex returns it to that same background environment so you can pick up where you left off.
96 102
97</div>103</div>
98 104
99<CodexScreenshot105<CodexScreenshot
100 alt="Handoff dialog moving a thread from a worktree to Local"106 alt="Handoff dialog moving a task from a worktree to Local"
101 lightSrc="/images/codex/app/handoff-light.webp"107 lightSrc="/images/codex/app/handoff-light.webp"
102 darkSrc="/images/codex/app/handoff-dark.webp"108 darkSrc="/images/codex/app/handoff-dark.webp"
103 maxHeight="400px"109 maxHeight="400px"
106 112
107</div>113</div>
108 114
109You can also go the other direction. If you're already working in Local and want to free up the foreground, use **Hand off** to move the thread to a worktree. This is useful when you want Codex to keep working in the background while you switch your attention back to something else locally.115You can also go the other direction. If you're already working in Local and want to free up the foreground, use **Hand off** to move the task to a worktree. This is useful when you want Codex to keep working in the background while you switch your attention back to something else locally.
110 116
111Since Handoff uses Git operations, any files that are part of your `.gitignore` file won't move with the thread unless Codex copies them into a local managed worktree with `.worktreeinclude`.117Since Handoff uses Git operations, any files that are part of your `.gitignore` file won't move with the task unless Codex copies them into a local managed worktree with `.worktreeinclude`.
112 118
113## Advanced details119## Advanced details
114 120
115### Codex-managed and permanent worktrees121### Codex-managed and permanent worktrees
116 122
117By default, threads use a Codex-managed worktree. These are meant to feel lightweight and disposable. A Codex-managed worktree is typically dedicated to one thread, and Codex returns that thread to the same worktree if you hand it back there later.123By default, tasks use a Codex-managed worktree. These are meant to feel lightweight and disposable. A Codex-managed worktree is typically dedicated to one task, and Codex returns that task to the same worktree if you hand it back there later.
118 124
119If you want a long-lived environment, create a permanent worktree from the three-dot menu on a project in the sidebar. This creates a new permanent worktree as its own project. Permanent worktrees aren't automatically deleted, and you can start multiple threads from the same worktree.125If you want a long-lived environment, create a permanent worktree from the three-dot menu on a project in the sidebar. This creates a new permanent worktree as its own project. Permanent worktrees aren't automatically deleted, and you can start multiple tasks from the same worktree.
120 126
121### How Codex manages worktrees for you127### How Codex manages worktrees for you
122 128
123Codex creates worktrees in `$CODEX_HOME/worktrees`. The starting commit will be the `HEAD` commit of the branch selected when you start your thread. If you chose a branch with local changes, the uncommitted changes will be applied to the worktree as well. The worktree will _not_ be checked out as a branch. It will be in a [detached HEAD](https://git-scm.com/docs/git-checkout#_detached_head) state. This lets Codex create several worktrees without polluting your branches.129Codex creates worktrees in `$CODEX_HOME/worktrees`. The starting commit is the `HEAD` commit of the branch selected when you start your task. If you chose a branch with local changes, Codex applies the uncommitted changes to the worktree as well. The worktree isn't checked out as a branch. It's in a [detached HEAD](https://git-scm.com/docs/git-checkout#_detached_head) state. This lets Codex create several worktrees without polluting your branches.
124 130
125### Copy ignored local files into managed worktrees131### Copy ignored local files into managed worktrees
126 132
137config/secrets.json143config/secrets.json
138```144```
139 145
140Codex skips source symlinks and won't overwrite files that already exist in the new checkout. This behavior applies to local Codex app managed worktrees, not remote worktrees or Git worktrees you create yourself from the command line.146Codex skips source symlinks and won't overwrite files that already exist in the new checkout. This behavior applies to local ChatGPT desktop app managed worktrees, not remote worktrees or Git worktrees you create yourself from the command line.
141 147
142### Branch limitations148### Branch limitations
143 149
149 155
150To resolve this, you would need to check out another branch instead of `feature/a` on the worktree.156To resolve this, you would need to check out another branch instead of `feature/a` on the worktree.
151 157
152If you plan on checking out the branch locally, use Handoff to move the thread into Local instead of trying to keep the same branch checked out in both places at once.158If you plan on checking out the branch locally, use Handoff to move the task into Local instead of trying to keep the same branch checked out in both places at once.
153 159
154<ToggleSection title="Why this limitation exists">160<ToggleSection title="Why this limitation exists">
155Git prevents the same branch from being checked out in more than one worktree at a time because a branch represents a single mutable reference (`refs/heads/<name>`) whose meaning is “the current checked-out state” of a working tree.161Git prevents the same branch from being checked out in more than one worktree at a time because a branch represents a single mutable reference (`refs/heads/<name>`) whose meaning is “the current checked-out state” of a working tree.
162 168
163### Worktree cleanup169### Worktree cleanup
164 170
165Worktrees can take up a lot of disk space. Each one has its own set of repository files, dependencies, build caches, etc. As a result, the Codex app tries to keep the number of worktrees to a reasonable limit.171Worktrees can take up a lot of disk space. Each one has its own set of repository files, dependencies, build caches, etc. As a result, the ChatGPT desktop app tries to keep the number of worktrees to a reasonable limit.
166 172
167By default, Codex keeps your most recent 15 Codex-managed worktrees. You can change this limit or turn off automatic deletion in settings if you prefer to manage disk usage yourself.173By default, Codex keeps your most recent 15 Codex-managed worktrees. You can change this limit or turn off automatic deletion in settings if you prefer to manage disk usage yourself.
168 174
169Codex tries to avoid deleting worktrees that are still important. Codex-managed worktrees won't be deleted automatically if:175Codex tries to avoid deleting worktrees that are still important. Codex-managed worktrees won't be deleted automatically if:
170 176
171- A pinned conversation is tied to it177- A pinned conversation is tied to it
172- The thread is still in progress178- The task is still in progress
173- The worktree is a permanent worktree179- The worktree is a permanent worktree
174 180
175Codex-managed worktrees are deleted automatically when:181Codex-managed worktrees are deleted automatically when:
176 182
177- You archive the associated thread183- You archive the associated task
178- Codex needs to delete older worktrees to stay within your configured limit184- Codex needs to delete older worktrees to stay within your configured limit
179 185
180Before deleting a Codex-managed worktree, Codex saves a snapshot of the work on it. If you open a conversation after its worktree was deleted, you'll see the option to restore it.186Before deleting a Codex-managed worktree, Codex saves a snapshot of the work on it. If you open a conversation after its worktree was deleted, you'll see the option to restore it.
182## Frequently asked questions188## Frequently asked questions
183 189
184<ToggleSection title="Can I control where worktrees are created?">190<ToggleSection title="Can I control where worktrees are created?">
185 Not today. Codex creates worktrees under `$CODEX_HOME/worktrees` so it can191 Yes. Codex creates managed worktrees under `$CODEX_HOME/worktrees` by
186 manage them consistently.192 default. To choose another location, open **Settings > Worktrees** and change
193 **Worktree root**.
187</ToggleSection>194</ToggleSection>
188 195
189<ToggleSection title="Can I move a thread between Local and Worktree?">196<a id="can-i-move-a-chat-between-local-and-worktree"></a>
190 Yes. Use **Hand off** in the thread header to move a thread between your local197
198<ToggleSection title="Can I move a task between Local and Worktree?">
199 Yes. Use **Hand off** in the task header to move a task between your local
191 checkout and a worktree. Codex handles the Git operations needed to move the200 checkout and a worktree. Codex handles the Git operations needed to move the
192 thread safely between environments. If you hand a thread back to a worktree201 task safely between environments. If you hand a task back to a worktree later,
193 later, Codex returns it to the same associated worktree.202 Codex returns it to the same associated worktree.
194</ToggleSection>203</ToggleSection>
195 204
196<ToggleSection title="What happens to threads if a worktree is deleted?">205<a id="what-happens-to-chats-if-a-worktree-is-deleted"></a>
197 Threads can remain in your history even if the underlying worktree directory206
198 is deleted. For Codex-managed worktrees, Codex saves a snapshot before207<ToggleSection title="What happens to tasks if a worktree is deleted?">
199 deleting the worktree and offers to restore it if you reopen the associated208 Tasks can remain in your history even if the underlying worktree directory is
200 thread. Permanent worktrees are not automatically deleted when you archive209 deleted. For Codex-managed worktrees, Codex saves a snapshot before deleting
201 their threads.210 the worktree and offers to restore it if you reopen the associated task.
211 Permanent worktrees are not automatically deleted when you archive their
212 tasks.
202</ToggleSection>213</ToggleSection>