codex - 💡(How to fix) Fix Codex Desktop should guide Git initialization before worktree creation [1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openai/codex#17474Fetched 2026-04-12 13:28:05
View on GitHub
Comments
1
Participants
1
Timeline
3
Reactions
1
Author
Participants
Timeline (top)
labeled ×2commented ×1

Error Message

  • if the repo root is a suspicious parent directory, warn the user and ask whether to use the selected folder as a separate project;

Code Example

fatal: invalid reference: HEAD
RAW_BUFFERClick to expand / collapse

Codex Desktop worktree creation has poor behavior for newly opened folders that are not initialized Git repositories or have no commits.

Expected behavior: When a user opens a normal project folder and clicks the UI button to create a worktree/branch, Codex should either:

  1. detect that the folder is not a Git repo and offer to initialize it, create or suggest a sensible .gitignore, and make the first baseline commit; or
  2. show a clear guided message explaining that the project needs Git initialization and a first commit before worktrees can be created.

Actual behavior: The UI can hide the worktree button or fail with low-level Git errors such as:

fatal: invalid reference: HEAD

Local reproduction/context:

  • A new project folder at /Users/sergey/ai/bene_rus_v2 was not a Git repository at all, so the user could not create a worktree through the expected UI flow.
  • An accidental empty parent repository at /Users/sergey/.git also caused child folders to be misdetected as part of that empty repo, so Codex tried to create a worktree from a non-existent HEAD.
  • The user expected to open a project folder and use the UI button, not manually diagnose Git state.

Why this matters: A normal user should not need to understand Git internals, run git init manually, create a .gitignore, make a first commit, or diagnose HEAD/reference errors just to use Codex's worktree feature.

Suggested fix: Add a preflight check before worktree creation:

  • detect whether the selected folder is inside a Git repository;
  • detect whether HEAD resolves to a valid commit;
  • detect whether the resolved Git root is unexpectedly a parent directory such as the user's home folder;
  • if no repo exists, offer an "Initialize Git project" flow;
  • if a repo exists but has no commits, offer a "Create initial commit" flow;
  • if the repo root is a suspicious parent directory, warn the user and ask whether to use the selected folder as a separate project;
  • only run git worktree add after HEAD is valid.

The UI should surface this as a guided setup flow, not as raw Git errors or a missing worktree button.

extent analysis

TL;DR

Implement a preflight check before worktree creation to detect and handle cases where the selected folder is not a Git repository or has no commits.

Guidance

  • Add a check to detect whether the selected folder is inside a Git repository before attempting to create a worktree.
  • Verify the existence of a valid HEAD reference to ensure the repository has at least one commit.
  • Implement a guided setup flow to handle cases where the repository does not exist or has no commits, offering options to initialize the Git project or create an initial commit.
  • Consider warning the user if the detected Git root is a suspicious parent directory, such as the user's home folder.

Example

import subprocess

def check_git_repo(folder):
    try:
        subprocess.run(["git", "-C", folder, "rev-parse", "--is-inside-work-tree"], check=True)
        return True
    except subprocess.CalledProcessError:
        return False

def check_head_reference(folder):
    try:
        subprocess.run(["git", "-C", folder, "rev-parse", "HEAD"], check=True)
        return True
    except subprocess.CalledProcessError:
        return False

Notes

The suggested fix relies on the git command-line tool being available and properly configured on the system. The implementation details may vary depending on the programming language and framework used by Codex.

Recommendation

Apply the suggested preflight check workaround to improve the user experience and handle edge cases where the selected folder is not a Git repository or has no commits. This approach provides a more guided and user-friendly setup flow, reducing the need for manual Git commands and error diagnosis.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING