Fixing the Git 'Dubious Ownership' Error in Docker and CI/CD

intermediateπŸ“¦ Git2026-06-29| Docker containers, GitHub Actions, GitLab CI/CD, or Linux environments running Git v2.35.2 or newer.

Error Message

fatal: detected dubious ownership in repository at '/workspace'
#git#docker#ci-cd#devops#security

The Scenario: When Git Gets Too ProtectiveIt happens in an instant. You trigger a build inside a Docker container or a CI/CD pipeline, expecting a smooth deployment. Your source code is mounted, the environment is ready, and you run a standard command like git status. Instead of a file list, Git crashes with a security warning:

fatal: detected dubious ownership in repository at '/workspace'

This isn't a random bug. It’s a security feature introduced in Git v2.35.2 to patch CVE-2022-24765. This vulnerability could allow a malicious actor to execute code by placing a rogue .git directory in a shared folder. Now, Git refuses to touch any repository where the current user doesn't own the .git folder.

Why This HappensGit now performs a strict ownership check. In a typical Docker setup, you might mount a local project folder owned by your host user (often UID 1000) into a container where the process runs as root (UID 0). Because the UIDs don't match, Git assumes the repository is untrustworthy. It blocks all operations to prevent potential exploits in shared environments.

Solution 1: The Targeted FixThe most precise way to resolve this is to explicitly tell Git that a specific directory is safe. This approach preserves security for everything else while unblocking your project. Run this command inside your container or runner:

git config --global --add safe.directory /workspace

Ensure you replace /workspace with the exact path shown in your error message. This command updates your global .gitconfig, creating a persistent exception for that path.

Solution 2: The "Catch-All" for CI/CDAutomated pipelines often use dynamic paths or multiple submodules. In an ephemeral environment like a GitHub Actions runner, you might not want to whitelist paths one by one. If the environment is isolated and you trust the source code, use the wildcard fix:

git config --global --add safe.directory '*'

Use with caution. Only apply the '*' wildcard in short-lived, isolated environments. Never run this on a shared production server with multiple users, as it disables the security check entirely.

Solution 3: Implementation in Docker and CI Configs### Optimizing DockerfilesAvoid manual fixes by baking the configuration into your image. This ensures every container started from the image is ready for Git operations immediately:

FROM alpine:3.18
RUN apk add --no-cache git
# Pre-approve the common workspace path to avoid ownership errors
RUN git config --global --add safe.directory /app
WORKDIR /app
COPY . .

Streamlining GitHub ActionsIf your custom scripts or third-party actions fail, insert a configuration step early in your workflow. It takes less than a second to run and prevents downstream failures:

- name: Mark workspace as safe
  run: git config --global --add safe.directory ${GITHUB_WORKSPACE}

- name: Generate build metadata
  run: git describe --tags --always

Solution 4: Changing File OwnershipYou can also align file ownership with the current user. This is the "Linux way" of solving the problem without changing Git settings. Inside the container, execute:

chown -R $(id -u):$(id -g) /workspace

Think twice before using this on mounted volumes. Since Docker shares the underlying file system, changing ownership inside the container will change it on your host machine too. This often leads to "Permission Denied" errors when you return to your IDE or text editor.

Verifying the FixConfirm that Git has registered your changes by listing all active safe directories. Run this command:

git config --global --get-all safe.directory

If the output correctly displays your path or the * symbol, your git status or git fetch commands will now work perfectly. The fix remains active as long as the ~/.gitconfig file persists in your environment.

Related Error Notes