0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Codex CLIでworktree作成時に.envを自動コピーする

0
Posted at

Claude Codeでは claude -w と .worktreeinclude を使うことで、.env などGit管理外のファイルをworktreeに引き継げます。

一方、Codex CLIの codex --worktree では、現状 .worktreeinclude は反映されません。

例えば以下のように書いていても、

# .worktreeinclude
.env

Codexが作成したworktreeには .env が存在しません。

~/.codex/worktrees/1234/sample-project

$ ls .env
ls: .env: No such file or directory

そこで、Codex用の簡単なwrapperを作ります。

#!/usr/bin/env bash
set -euo pipefail

ROOT="$(git rev-parse --show-toplevel)"
NAME="${1:-codex-$(date +%s)}"
WT="$HOME/.codex-worktrees/$(basename "$ROOT")/$NAME"

git worktree add "$WT" -b "$NAME"

while IFS= read -r f; do
  [[ -z "$f" || "$f" =~ ^# ]] && continue
  mkdir -p "$WT/$(dirname "$f")"
  cp -p "$ROOT/$f" "$WT/$f"
done < "$ROOT/.worktreeinclude"

cd "$WT"
exec codex

例えば ~/.local/bin/cxw として保存します。

chmod +x ~/.local/bin/cxw

~/.local/bin がPATHに入っていなければ、~/.zshrc に追加します。

export PATH="$HOME/.local/bin:$PATH"

これで、

cxw feature/foo

と実行すると、

worktree作成
↓
.worktreeinclude に書かれたファイルをコピー
↓
そのworktree内でCodexを起動

まで自動化できます。

.worktreeinclude はそのまま共通で使えるので、

.env
.env.local
apps/api/.env

のようにしておけば、Claude CodeとCodexで同じ設定を共有できます。

現状、Codex CLIでClaude Codeの claude -w に近い使い勝手にしたい場合、このwrapper方式がシンプルです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?