Repository navigation
fix(claude-code): set git safe.directory and gh credential helper via env - #203
Merged
Merged
Conversation
… env VS Code's Git extension intermittently fails with "dubious ownership" on the bind-mounted /workspace. The Dev Containers extension adds safe.directory to ~/.gitconfig only when its one-time ownership check at attach detects a mismatch, and Docker Desktop's reported ownership changes over time. Set git config through GIT_CONFIG_COUNT in containerEnv instead. Git reads it as command scope, which is protected (so safe.directory is honored) and outranks the system and global files the extension writes: - safe.directory=/workspace - rewrite git@github.com: remotes to HTTPS - reset the github.com credential helper list, then add `gh auth git-credential`. This is the same pair `gh auth setup-git` writes. Without the reset, VS Code's injected helper answers first with the host's possibly stale credential. Closes #172
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Sets git config for every process in
claude-codetemplate containers throughGIT_CONFIG_COUNTincontainerEnv. This fixes the intermittent "dubious ownership" error on/workspaceand letsgit@github.com:remotes authenticate through the container'sghlogin.Why
Closes #172.
safe.directoryto~/.gitconfigonly when its one-time ownership check at attach detects a mismatch (see Dev Containers fails safe.directory detection when folder path contains square brackets on Windows microsoft/vscode-remote-release#11021). Docker Desktop's reported ownership of the bind mount changes over time, so VS Code's Git extension fails some of the time.ghonly authenticates HTTPS, and the forwarded ssh-agent often has no keys (Secretive, 1Password, on-disk keys)./etc/gitconfigand~/.gitconfig, which answers first with the host's GitHub credential. That credential may be stale.Env-based config is git's command scope. Per
git-config(1), that scope counts as protected configuration, so git honorssafe.directorythere. It also outranks the system and global files the extension writes. The two credential entries, an empty helper followed by!gh auth git-credential, are exactly whatgh auth setup-gitwrites (cli/clihelper_config.go). It's gh's own way to "sever the chain of credential helpers", andgitcredentials(7)documents the empty value as a reset.Changes
claude-code/.devcontainer/devcontainer.jsonandclaude-sandbox/devcontainer.json: fourGIT_CONFIG_*entries incontainerEnv:safe.directory=/workspaceurl.https://github.com/.insteadOf=git@github.com:credential.https://github.com.helper, then!gh auth git-credentialclaude-code/README.md: new "Git and GitHub Authentication" section covering each entry, the one-timegh auth login, how to inspect the config with--show-scope, and how to add entries. A link to it from "Key settings included".Verification
Ran in
ghcr.io/gatezh/devcontainers/claude-code:latest(git 2.47.3)./workspacewas owned by UID 4242, and a stand-in for VS Code's credential helper was set in both/etc/gitconfigand~/.gitconfig. The env values were parsed from each edited JSONC file:git statusin/workspacefatal: detected dubious ownershipgit ls-remote --get-url git@github.com:owner/repo.githttps://github.com/owner/repo.gitgit credential fillfor github.comgit credential fillfor gitlab.comAlso confirmed: with only the
ghhelper and no empty reset, github.com still gets the stale host credential.Notes
.devcontainer/(thedevcontainer-upstream-syncskill) and rebuilding. No image rebuild is needed for the fix itself.git@github.com:remotes now always go over HTTPS throughgh, even when the forwarded ssh-agent would have worked. The host's remotes and SSH setup don't change..devcontainer/has no gh config volume, so it isn't changed here.ssh://git@github.com/remotes and gist hosts aren't covered.