Skip to content

Branch display shows main repo branch instead of worktree branch when opening a git worktree directory #16474

Closed as not planned

Description

@ke7in-zz

Description

When opening a git worktree directory directly in the OpenCode desktop app, the branch displayed in the UI shows the main repository's branch (e.g. dibble-dev) instead of the worktree's actual branch (e.g. i847761/PRD-manage-family).

Reproduction

  1. Have a git repo at e.g. /Projects/my-app on branch main
  2. Create a worktree: git worktree add /Projects/worktrees/my-app-feature feature-branch
  3. Open the worktree directory /Projects/worktrees/my-app-feature in the OpenCode desktop app
  4. Observe the branch indicator shows main instead of feature-branch

Root Cause

The worktree's .git file is a pointer:

gitdir: /Projects/my-app/.git/worktrees/my-app-feature

OpenCode appears to follow git rev-parse --git-common-dir (which returns /Projects/my-app/.git) and reads HEAD from there — getting the main repo's branch. It should instead read the worktree-specific HEAD at .git/worktrees/my-app-feature/HEAD.

Additionally, the project table in OpenCode's SQLite database (~/.local/share/opencode/opencode.db) stores the worktree column as the main repo path rather than the actual worktree directory, even when the worktree directory is opened directly.

Evidence from the database:

project.worktree = /Projects/my-app          (main repo — incorrect)
session.directory = /Projects/worktrees/my-app-feature  (correct)

Running git rev-parse --abbrev-ref HEAD from each directory confirms:

/Projects/my-app                        → main
/Projects/worktrees/my-app-feature      → feature-branch

Suggested Fix

Use git rev-parse --abbrev-ref HEAD executed from the actual working directory (or session directory) rather than resolving through --git-common-dir. The project.worktree column should also be set to the worktree directory when one is opened directly.

Environment

  • OpenCode desktop app (macOS)
  • Git worktrees created via git worktree add

Activity

  1. ke7in-zz commented on Mar 7, 2026

    @ke7in-zz
    Author

    Additional Detail

    After patching the project.worktree column in the database and restarting, the correct worktree branch does appear briefly on startup, but then reverts back to the main repo branch mid-session (during agent thinking/processing).

    This suggests there is a background git polling or project refresh cycle that re-derives the branch by following --git-common-dir rather than staying scoped to the worktree's working directory. The initial resolution is correct, but it gets overwritten by a subsequent refresh.

  2. natewill commented on Mar 7, 2026

    @natewill
    Contributor

    will work on

  3. natewill commented on Mar 8, 2026

    @natewill
    Contributor

    PR coming in the morning

  4. removed
    webRelates to opencode on web / desktop
    on May 3, 2026
  5. github-actions commented on Jul 3, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions