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.

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:
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:
# 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/repoInspect what is currently trusted:
git config --global --get-all safe.directory
git config --show-origin --get-all safe.directory # which file set itKey Comparison Table
| Context | Why ownership differs | Fix |
|---|---|---|
| Docker bind mount | Host UID preserved, container runs as root | --user $(id -u):$(id -g), or safe.directory in the image |
| Named volume created by another image | Volume inherits the creating image's UID | chown in an entrypoint, or safe.directory |
| CI: clone as runner, build in container | Two UIDs on one workspace | Set safe.directory for the workspace path in the job |
sudo git … | Root running Git in a user-owned repo | Do not use sudo; fix the operation instead |
WSL accessing /mnt/c/... | Windows ACLs map to a different owner | Work inside the Linux filesystem, or trust the path |
| NFS/SMB share | UID mapping differs across hosts | Explicit uid=/gid= mount options |
| Prebuilt image with a copied repo | COPY sets ownership to root | COPY --chown=app:app . . |
| Suppression | Scope | Risk |
|---|---|---|
--add safe.directory <path> | One exact path, one user | Low |
safe.directory=/parent | Not applied to children the way you expect | Low, but ineffective |
safe.directory=* | Every repository, every owner | High: removes the protection entirely |
GIT_CONFIG_COUNT env pair | One process | Low, ideal for CI |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Dockerfile: the fix that travels with the image
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 . .
USER appsafe.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
services:
app:
build: .
user: "${UID:-1000}:${GID:-1000}"
volumes:
- .:/appUID=$(id -u) GID=$(id -g) docker compose upRunning 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
# GitHub Actions
- name: Trust workspace
run: |
git config --global --add safe.directory "$GITHUB_WORKSPACE"
git status --short# 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/workspaceThe 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:
git -C /app rev-parse --git-dir
# /repo/.git/worktrees/app
git config --global --add safe.directory /repoListing 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
git config --global --add safe.directory '*' # works, and disables the checkThe 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
sudo git -C /home/dev/project status # dubious ownership: root vs dev
sudo -u dev git -C /home/dev/project status # finesudo 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
| Mistake | Result | Correct approach |
|---|---|---|
safe.directory for a child path of the repository | Still fatal | Trust the --git-common-dir path |
| Setting it in the repository's own config | Rejected; the check runs before that config | Global, system, -c, or env |
'*' copied from a Stack Overflow answer | Protection removed | Narrow paths |
chown inside the entrypoint as non-root | Operation not permitted | Run the chown before dropping privileges |
Trusting /app but mounting at /workspace | Still fatal | Match the actual mount path |
| Assuming it is an npm/pip problem | Wasted debugging | Read 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 toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit config: Aliases, rerere, and Credential Helpers
Five config scopes decide one value at a time. Aliases are user-level shorthand, rerere records conflict resolutions, and credential helpers choose where your secret lives.
Git Hooks: pre-commit, commit-msg, and Husky Automation
Hooks live in .git/hooks, which a clone never receives, so core.hooksPath and Husky are how they travel. Exit codes abort the operation, and every hook is bypassable.
Git bisect: Binary Search, Automated Scripts, and Bug Hunting
Mark one bad and one good commit and Git halves the range each step. bisect run automates it through exit codes, and monotonicity is the assumption that must hold.
Git ignore: Patterns, Negation, and Untracking Cached Files
Patterns are relative and last match wins, but a negation cannot revive a file inside an excluded directory. Ignore rules never touch tracked files: rm --cached first.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.