Skip to content

Make git work in the dev container when opened from a worktree#2803

Merged
yahonda merged 1 commit into
rsim:masterfrom
yahonda:devcontainer-worktree-git-dir
May 19, 2026
Merged

Make git work in the dev container when opened from a worktree#2803
yahonda merged 1 commit into
rsim:masterfrom
yahonda:devcontainer-worktree-git-dir

Conversation

@yahonda
Copy link
Copy Markdown
Collaborator

@yahonda yahonda commented May 19, 2026

Summary

When the dev container is opened from a git worktree (e.g. <repo>/.worktrees/<branch>/), the worktree's .git is a file whose gitdir: line points at a host path under the main repo's .git/worktrees/<branch>. That host path is not visible inside the container by default, so git inside the container fails with fatal: not a git repository: (null) — VS Code's safe.directory probe, credential.helper setup, and any developer git command all hit this. The dev container build itself completes, but the in-container git workflow is broken.

initializeCommand.sh already runs on the host, where git is available. Resolve the common git dir via git rev-parse --git-common-dir, write the absolute path into .devcontainer/.env, and have docker-compose.yml bind-mount that host path at the same path inside the container. The worktree's .git file's gitdir: reference then resolves cleanly.

For a main checkout the host path is <repo>/.git, and mounting it at <repo>/.git inside the container is a redundant no-op-shaped mount that doesn't conflict with the existing workspace mount.

Changes

  • .devcontainer/initializeCommand.sh — after the TZ extraction, compute GIT_COMMON_DIR and write .devcontainer/.env.
  • .devcontainer/docker-compose.yml — bind-mount ${GIT_COMMON_DIR}:${GIT_COMMON_DIR}:cached on the app service. docker-compose reads .env from the directory containing docker-compose.yml automatically.
  • .gitignore — ignore .devcontainer/.env (generated per launch).

Test plan

  • Open the dev container from a worktree (<repo>/.worktrees/<branch>/) and confirm:
    • The dev container builds without "fatal: not a git repository" lines during safe.directory and credential.helper setup.
    • In an interactive terminal inside the container: git status succeeds and git rev-parse --show-toplevel prints /workspaces/oracle-enhanced.
  • Open the dev container from the main checkout (<repo>/) and confirm the same — the extra bind mount should be harmless.

When the dev container is opened from a git worktree, the worktree's
.git is a file whose `gitdir:` line points at a host path under the
main repo's .git/worktrees/<branch>. That host path is not visible
inside the container by default, so git inside the container fails
with "fatal: not a git repository: (null)" -- VS Code's safe.directory
probe, credential.helper setup, and any developer git command all hit
this.

Resolve the common git dir on the host in initializeCommand.sh via
`git rev-parse --git-common-dir`, write the absolute path into
.devcontainer/.env, and have docker-compose.yml bind-mount it at the
same path inside the container. The worktree's `.git` file's gitdir
reference then resolves cleanly. For a main checkout this is a
no-op-shaped redundant mount of <repo>/.git at the same host path
inside the container; nothing breaks.

.devcontainer/.env is generated per-launch and is gitignored.
@yahonda yahonda merged commit 429aa95 into rsim:master May 19, 2026
12 checks passed
@yahonda yahonda deleted the devcontainer-worktree-git-dir branch May 19, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant