Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Git detected dubious ownership: Docker, Compose, and CI Fixes

A bind mount keeps the host UID and the container runs as root, so Git stops before reading a single object. Trust the path Git resolves, not the one you mounted.

~5 min readGit
#Git#Docker#CI#Security
3D illustration of a repository cube behind an ownership gate with two user key cards

Introduction & Quick Summary

Symptom. Any Git command in a container or on a mounted volume stops with fatal: detected dubious ownership in repository at '/app' and the hint To add an exception for this directory, call: git config --global --add safe.directory /app. Nothing in the repository is broken, git status fails before it looks at a single object, and CI pipeline steps that worked last month now fail with exit code 128.

Cause. The directory's owner is not the user running Git, and the owner differs from the user that owns it in Git's record. Git checks repository ownership before reading configuration from it, because a repository owned by someone else could carry hooks or config that run as you. This check arrived as a fix for CVE-2022-24765 and is present in Git 2.35.2+ and in patched 2.30+ releases. Bind mounts (-v $PWD:/app) preserve the host UID, containers run as root or as a fixed UID, CI runners clone as one user and build as another, and WSL or a network share changes the owner — all produce the error.

Fix. Either make ownership match (chown -R $(id -u):$(id -g) /app), or declare the path trusted for the user running Git (git config --global --add safe.directory /app). In containers the cleanest answer is safe.directory inside the image or the job, scoped to the exact path, and never *.

Core Mechanics, Signatures & Reproducible Setup

Git resolves the repository's .git directory, then compares the owner of that directory with the effective UID of the process. If they differ and the path is not listed in safe.directory, it refuses with the fatal message. The check reads protected configuration, so the exception itself must come from a source Git trusts: system config, global config, -c, or the GIT_CONFIG_* environment variables. A repository's own config cannot grant the exception.

Reproduce it deterministically:

bash
mkdir -p /tmp/owner-demo/repo && cd /tmp/owner-demo/repo git init -q && git commit -q --allow-empty -m init sudo chown -R 12345:12345 /tmp/owner-demo/repo git status # fatal: detected dubious ownership in repository at '/tmp/owner-demo/repo'

Two fixes, in order of preference:

bash
# 1. Make ownership match (best: the check passes for the right reason) sudo chown -R "$(id -u):$(id -g)" /tmp/owner-demo/repo # 2. Declare this path trusted for this user git config --global --add safe.directory /tmp/owner-demo/repo

Inspect what is currently trusted:

bash
git config --global --get-all safe.directory git config --show-origin --get-all safe.directory # which file set it

Key Comparison Table

ContextWhy ownership differsFix
Docker bind mountHost UID preserved, container runs as root--user $(id -u):$(id -g), or safe.directory in the image
Named volume created by another imageVolume inherits the creating image's UIDchown in an entrypoint, or safe.directory
CI: clone as runner, build in containerTwo UIDs on one workspaceSet safe.directory for the workspace path in the job
sudo git …Root running Git in a user-owned repoDo not use sudo; fix the operation instead
WSL accessing /mnt/c/...Windows ACLs map to a different ownerWork inside the Linux filesystem, or trust the path
NFS/SMB shareUID mapping differs across hostsExplicit uid=/gid= mount options
Prebuilt image with a copied repoCOPY sets ownership to rootCOPY --chown=app:app . .
SuppressionScopeRisk
--add safe.directory <path>One exact path, one userLow
safe.directory=/parentNot applied to children the way you expectLow, but ineffective
safe.directory=*Every repository, every ownerHigh: removes the protection entirely
GIT_CONFIG_COUNT env pairOne processLow, ideal for CI

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Dockerfile: the fix that travels with the image

dockerfile
FROM node:22-bookworm-slim # git is not installed in slim images; add it only if the build needs it RUN apt-get update && apt-get install -y --no-install-recommends git \ && rm -rf /var/lib/apt/lists/* ARG APP_UID=10001 RUN git config --system --add safe.directory /app \ && useradd -u "$APP_UID" -m app WORKDIR /app COPY --chown=app:app . . USER app

safe.directory set with --system applies to every user in the image, which is correct for a path the image itself owns. Pairing it with COPY --chown means the reverse case — a mounted volume that replaces /app — is the only one left, and volume mounts are usually handled by an entrypoint chown or by matching host and container UIDs.

2. docker-compose with a bind mount

yaml
services: app: build: . user: "${UID:-1000}:${GID:-1000}" volumes: - .:/app
bash
UID=$(id -u) GID=$(id -g) docker compose up

Running as the host user removes the mismatch at the source. If the container needs root for another reason, mount the repository at a path listed in the image's safe.directory and accept that Git will consider it trusted.

3. CI: two UIDs on one workspace

yaml
# GitHub Actions - name: Trust workspace run: | git config --global --add safe.directory "$GITHUB_WORKSPACE" git status --short
yaml
# Container job where the global config is not the right scope env: GIT_CONFIG_COUNT: "1" GIT_CONFIG_KEY_0: safe.directory GIT_CONFIG_VALUE_0: /github/workspace

The GIT_CONFIG_* triple is the most portable form: it needs no writable home directory, works in a distroless image with only git, and applies to that process only. Use it when the image has no $HOME (common for gcr.io/cloud-builders/git-style images).

4. Worktrees and bare repositories

A linked worktree's .git is a file pointing at the main repository's .git/worktrees/<name> directory. The ownership check applies to the repository directory Git resolves, so a worktree created by one user and opened by another needs the main repository path trusted:

bash
git -C /app rev-parse --git-dir # /repo/.git/worktrees/app git config --global --add safe.directory /repo

Listing only /app leaves the error in place, because Git's check is against /repo. git rev-parse --git-common-dir prints the shared path; trust that one.

5. Why safe.directory=* is a real trade-off

bash
git config --global --add safe.directory '*' # works, and disables the check

The check exists because a repository's .git/hooks and .git/config execute code as the invoking user. On a shared machine or a workspace that other people can write to, * restores the exact vulnerability the patch closed. On an ephemeral CI runner with a single-tenant workspace it is defensible; on a developer laptop it is not. If you need a broad exception, prefer a narrow one per project root and script it.

6. The same command inside sudo

bash
sudo git -C /home/dev/project status # dubious ownership: root vs dev sudo -u dev git -C /home/dev/project status # fine

sudo git also rewrites the config scope it reads, so a fix applied with git config --global as your user does not help the root invocation. Running as the owning user is the correct answer; sudo -u is the smallest change that keeps both behaviours.

Common Compilation/Runtime Mistakes & Version Gotchas

MistakeResultCorrect approach
safe.directory for a child path of the repositoryStill fatalTrust the --git-common-dir path
Setting it in the repository's own configRejected; the check runs before that configGlobal, system, -c, or env
'*' copied from a Stack Overflow answerProtection removedNarrow paths
chown inside the entrypoint as non-rootOperation not permittedRun the chown before dropping privileges
Trusting /app but mounting at /workspaceStill fatalMatch the actual mount path
Assuming it is an npm/pip problemWasted debuggingRead the fatal line: it names the path

Version notes: the ownership check ships in Git 2.35.2 and later and was backported into several 2.30–2.34 patch releases, which is why an old distro package started failing after a routine update. The safe.directory exception reads only from protected configuration, so a repository cannot grant it to itself. git config --global --add appends a duplicate on every run — harmless for the check, but --get-all output grows; use --replace-all in Dockerfiles that rebuild.

For the surrounding work, the YAML validator catches the user: key in a compose file that inherits the wrong UID, the text diff tool compares two git config --list --show-origin dumps when a setting exists in CI but not locally, and the hash tool is handy when a CI image is pinned by digest and you need to confirm both jobs use the same one.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.