Skip to content

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

@ke7in-zz

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

Metadata

Metadata

Assignees

Labels

webRelates to opencode on web / desktop

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions