|
| 1 | +--- |
| 2 | +tags: ["codex", "git", "code review", "cli"] |
| 3 | +categories: ["tooling", "git"] |
| 4 | +title: "Using Codex to Review Code Changes Between Branches (Feature vs. Main)" |
| 5 | +image: |
| 6 | + path: /assets/img/2026-02-09/main.jpg |
| 7 | + alt: All your code are belong to us! |
| 8 | +--- |
| 9 | + |
| 10 | +I love code review. I also love _finishing_ code review. And if you’ve ever stared at a PR thinking “surely I’m missing something obvious”, you already know the real problem: our brains are not great at diff-scrolling for an hour straight. |
| 11 | + |
| 12 | +This post is my practical workflow for reviewing changes **between a feature branch and `main`** with **OpenAI Codex** (the CLI + agent), without needing to open 37 browser tabs. |
| 13 | + |
| 14 | +## Step 0: Make sure you’re diffing the right thing |
| 15 | + |
| 16 | +When people say “compare my feature branch to main”, they usually mean: |
| 17 | + |
| 18 | +- “Show me what my branch changes _since it diverged from `main`_ (the merge base).” |
| 19 | + |
| 20 | +In Git, that’s the **three-dot** form: |
| 21 | + |
| 22 | +```terminal |
| 23 | +# From your feature branch |
| 24 | +git fetch origin |
| 25 | +git diff --stat origin/main...HEAD |
| 26 | +git diff origin/main...HEAD |
| 27 | +``` |
| 28 | + |
| 29 | +That `A...B` syntax is special: |
| 30 | + |
| 31 | +- Git first finds the last shared commit where both branches were still the same (the **merge base**). |
| 32 | +- Then it shows everything that changed from that shared point to `B`. |
| 33 | + |
| 34 | +So `origin/main...HEAD` means: “Start where my branch split from `main`, then show only what my current branch added.” |
| 35 | +That is usually the exact view you want for branch/PR review. |
| 36 | + |
| 37 | +## Step 1: Run a local Codex review (my favorite way) |
| 38 | + |
| 39 | +Start Codex in your repo: |
| 40 | + |
| 41 | +```terminal |
| 42 | +codex |
| 43 | +``` |
| 44 | + |
| 45 | +Then run a review preset: |
| 46 | + |
| 47 | +1. Type: |
| 48 | + |
| 49 | +```terminal |
| 50 | +/review |
| 51 | +``` |
| 52 | + |
| 53 | +2. Pick **Review against a base branch** |
| 54 | +3. Choose your base branch (usually `main`) |
| 55 | + |
| 56 | +Codex will figure out the merge base (against the base branch’s upstream) and review the diff. This is the closest thing I’ve found to “review my PR, but locally, right now”. |
| 57 | + |
| 58 | +### My go-to prompt tweaks |
| 59 | + |
| 60 | +After the preset spins up, I usually add one short message so the review isn’t generic: |
| 61 | + |
| 62 | +- “Focus on correctness, edge cases, and missing tests.” |
| 63 | +- “Call out any API contract changes.” |
| 64 | +- “Flag risky refactors and backwards-incompatible behavior.” |
| 65 | +- “List the top 5 things you’d comment on in a PR, with file paths.” |
| 66 | + |
| 67 | +## Step 2: Ask Codex to summarize the change like a PR description |
| 68 | + |
| 69 | +This sounds silly, but it catches problems fast because it forces the reviewer (human or AI) to build a coherent mental model. |
| 70 | + |
| 71 | +Ask for: |
| 72 | + |
| 73 | +- A 5–10 bullet summary (“what changed”) |
| 74 | +- A “why” hypothesis (“what this is trying to achieve”) |
| 75 | +- A checklist of tests to run |
| 76 | +- A list of “areas of risk” |
| 77 | + |
| 78 | +If the summary doesn’t match your intent, the diff is probably confusing… which means your teammates are about to have a bad time too. |
| 79 | + |
| 80 | +## Step 3: One-shot mode with `codex exec` (nice for scripts) |
| 81 | + |
| 82 | +If you want a “review pass” without an interactive session, `codex exec` can run an agentic task from your shell. |
| 83 | + |
| 84 | +Example idea: |
| 85 | + |
| 86 | +```terminal |
| 87 | +codex exec "Compare my current branch with origin/main and list: (1) likely bugs, (2) missing tests, (3) backwards-incompatible changes. Use file paths." |
| 88 | +``` |
| 89 | + |
| 90 | +I still prefer `/review` for deeper diffs, but `codex exec` is great for quick sanity checks (or when you’re wiring this into a local script). |
| 91 | + |
| 92 | +## Bonus: Reviewing GitHub PRs with Codex |
| 93 | + |
| 94 | +If your team lives in GitHub PRs, you can also use Codex there: comment with a mention (for example, `@codex review`) and ask for what you want (security pass, test gaps, style, etc.). It’s a different workflow than local `/review`, but it’s convenient when the conversation is already in the PR. |
| 95 | + |
| 96 | +## Gotchas (aka “why does this review feel wrong?”) |
| 97 | + |
| 98 | +### 1) Two-dot vs three-dot confusion |
| 99 | + |
| 100 | +- `main..feature` is “commits reachable from `feature` but not `main`” (fine for some things). |
| 101 | +- `main...feature` is “diff from merge base to `feature`” (usually what you want for branch review). |
| 102 | + |
| 103 | +If you’re ever unsure, run both `--stat` outputs and see which one matches the changes you actually made. |
| 104 | + |
| 105 | +### 2) Your base branch doesn’t track an upstream |
| 106 | + |
| 107 | +Codex’s base-branch review relies on the base branch’s upstream when computing the merge base. If your `main` isn’t tracking `origin/main`, fix that first: |
| 108 | + |
| 109 | +```terminal |
| 110 | +git branch -u origin/main main |
| 111 | +``` |
| 112 | + |
| 113 | +### 3) Huge diffs produce huge reviews |
| 114 | + |
| 115 | +If the diff is massive, ask Codex to review in passes: |
| 116 | + |
| 117 | +- “First: high-level architecture + public APIs.” |
| 118 | +- “Second: correctness and edge cases.” |
| 119 | +- “Third: tests, docs, and naming.” |
| 120 | + |
| 121 | +You can also scope it: “Only review `src/`” or “Ignore formatting-only changes.” |
| 122 | + |
| 123 | +## Wrap-up |
| 124 | + |
| 125 | +My “default” flow is: |
| 126 | + |
| 127 | +1. `git diff origin/main...HEAD` to sanity-check the size and shape of the change |
| 128 | +2. `codex` → `/review` → “Review against a base branch” |
| 129 | +3. Ask for a PR-style summary + test checklist |
| 130 | + |
| 131 | +It’s fast, it’s local, and it catches a shocking number of “oops” moments before anyone else sees them. |
| 132 | + |
| 133 | +## References |
| 134 | + |
| 135 | +- OpenAI Codex CLI docs (review presets, `/review`, `codex exec`): https://developers.openai.com/codex/cli |
| 136 | +- Git docs for `git diff` (including the `A...B` merge-base form): https://git-scm.com/docs/git-diff |
| 137 | +- OpenAI Help Center (Codex in GitHub, `@codex` usage): https://help.openai.com/en/collections/10677170-codex |
0 commit comments