Skip to content

How to run OpenAI Codex tasks in parallel using Git worktrees

Purpose

This post demonstrates how to run multiple OpenAI Codex tasks in parallel on the same codebase without conflicts using Git worktrees.

Environment

  • Git 2.40+
  • OpenAI Codex API
  • Multiple development tasks on same repository

The Problem

How do you run multiple OpenAI Codex tasks simultaneously on the same codebase?

Traditional AI code assistants work sequentially. You run one task, switch branches, run another task, repeat. This is slow and creates friction. When I tried running parallel Codex tasks in the same directory, files got overwritten and changes merged chaotically.

The core issue: OpenAI Codex generates code and modifies files. If two tasks touch the same file system simultaneously, conflicts happen. Branch switching doesn’t help because you still have one working directory.

I needed a way to:

  • Run multiple Codex tasks at the same time
  • Keep each task isolated until completion
  • Review each task independently
  • Merge completed tasks without conflicts

The Solution: Git Worktrees

Git worktrees solve this by providing multiple working directories linked to the same repository. Each worktree can have different branches checked out simultaneously. Changes in one worktree don’t affect others.

I set up three parallel Codex tasks using worktrees:

Creating parallel worktrees
# Main repository
cd /path/to/main-project
# Create worktrees for three parallel Codex tasks
git worktree add ../project-task-auth feature/auth-implementation
git worktree add ../project-task-db feature/migration-scripts
git worktree add ../project-task-ui fix/responsive-layout
# Verify worktrees
git worktree list
# Output:
# /path/to/main-project feature/main-branch
# /path/to/project-task-auth feature/auth-implementation
# /path/to/project-task-db feature/migration-scripts
# /path/to/project-task-ui fix/responsive-layout

Each worktree is a separate directory. I ran Task 1 in ../project-task-auth, Task 2 in ../project-task-db, and Task 3 in ../project-task-ui. All three ran simultaneously without interference.

How It Works

Git Worktrees Foundation

A Git worktree is a separate working directory linked to the same .git repository. When you create a worktree:

Terminal window
git worktree add ../project-task-auth feature/auth-implementation

Git creates a new directory ../project-task-auth with:

  • Its own working copy of files
  • The branch feature/auth-implementation checked out
  • A link to the original repository’s .git directory

You can have multiple worktrees checked out to different branches at the same time. Changes in one worktree stay isolated until you commit and merge.

OpenAI Codex Task-Based Execution

OpenAI Codex works best with task-based prompts rather than continuous chat sessions. Each task is self-contained:

Codex task prompt structure
Task: Implement OAuth2 authentication with Google provider
Context:
- Worktree: ../project-task-auth
- Base branch: feature/auth-implementation
- Dependencies: None (isolated task)
Requirements:
1. Add Google OAuth2 configuration
2. Implement callback handler
3. Add user session management
4. Update environment variables template
5. Add unit tests for auth flow
Output:
- All changes within this worktree
- Follow-up fixes within this task context
- Single cohesive diff for review

I ran this task entirely within the ../project-task-auth worktree. Codex planned, executed, and made follow-up changes all in that isolated directory.

Parallel Execution

Here’s the parallel workflow I used:

Task 1: Auth feature
cd ../project-task-auth
# OpenAI Codex operates entirely within this worktree
# Planning happens here
# Execution happens here
# Follow-up changes happen here
# Task completes -> produces clean diff
git diff feature/main-branch
# Shows only auth-related changes, isolated from other tasks
Task 2: Database migration (parallel)
cd ../project-task-db
# Runs simultaneously with Task 1 and Task 3
# Completely isolated from auth feature work
# No conflicts or interference
# Task completes -> independently reviewable
git diff feature/main-branch
# Shows only database migration changes
Task 3: UI fix (parallel)
cd ../project-task-ui
# Runs in parallel with Tasks 1 and 2
# Isolated from both auth and database work

All three tasks ran at the same time. Each had its own working directory, so no file conflicts occurred.

Integration Phase

When tasks completed, I reviewed each independently:

Review and merge
# Each task reviewed separately
cd ../project-task-auth
git checkout feature/main-branch
git merge feature/auth-implementation
cd ../project-task-db
git checkout feature/main-branch
git merge feature/migration-scripts
# Clean up completed worktrees
git worktree remove ../project-task-auth
git worktree remove ../project-task-db

Each merge produced a clean diff because changes were isolated in separate worktrees.

Why This Works

The key insight is that Git worktrees provide physical isolation while task-based execution provides logical isolation. Together they enable:

Speed: Run multiple AI tasks simultaneously instead of sequentially. I ran three tasks in parallel that would have taken 3x longer sequentially.

Isolation: No interference between parallel tasks. Each worktree has its own file system, so Codex can’t accidentally modify files from another task.

Reviewability: Each task produces a clean, independent diff. When Task 1 completed, I reviewed git diff feature/main-branch and saw only auth-related changes.

Safety: Experimental work doesn’t destabilize main development. If a task fails or produces bad code, I delete the worktree and start over.

Resource Efficiency: Multiple tasks share the same .git directory. Worktrees use less disk space than separate clones.

Common Mistakes

I tried a few wrong approaches before worktrees:

Mistake 1: Running parallel tasks in same directory → Conflicts and overwritten changes. Codex Task 1 modified auth.ts while Task 2 tried to read it, causing conflicts.

Mistake 2: Using separate clones instead of worktrees → Wasted disk space, out-of-sync repos. Three clones of a 500MB repo used 1.5GB. Three worktrees used ~600MB.

Mistake 3: Treating Codex as continuous chat rather than task-based → Poor isolation, hard to review. When I tried a single long conversation with multiple requests, changes got mixed together and I couldn’t separate them.

Mistake 4: Not planning task boundaries → Tasks overlapped, creating merge conflicts. Once I had Task 1 modify User model and Task 2 add migrations for the same model, causing a merge conflict.

Mistake 5: Forgetting to clean up worktrees → Accumulated stale directories. I had 15 old worktrees before I learned to run git worktree prune regularly.

The Reason

Git worktrees enable parallel OpenAI Codex tasks because:

  1. Separate file systems: Each worktree has its own working directory, so parallel tasks can’t overwrite each other’s files.
  2. Shared Git history: All worktrees link to the same .git directory, so merges work normally.
  3. Branch isolation: Each worktree checks out a different branch, keeping commits separate until merge.

OpenAI Codex’s task-based model complements this by:

  1. Self-contained prompts: Each task has clear requirements and output expectations.
  2. Follow-up handling: Codex makes iterative improvements within the same task context, not across tasks.
  3. Reviewable output: Each task produces a cohesive diff that can be reviewed independently.

Summary

In this post, I showed how to run multiple OpenAI Codex tasks simultaneously on the same codebase using Git worktrees. The key point is that worktrees provide physical isolation for parallel tasks, while task-based execution provides logical isolation. Together they enable efficient, reviewable parallel AI-assisted development.

You can see that I succeeded to run three Codex tasks in parallel without conflicts, each producing clean, independently reviewable changes. The workflow transforms AI-assisted development from sequential assistance to parallel task execution.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments