GitLab CI "fatal: detected dubious ownership in repository": how to fix it

By Melih Toksari, developer of MergeBell · Updated · 3 min read

Short answer

Git refuses to work in a repository owned by a different user than the one running it, and says fatal: detected dubious ownership in repository. In GitLab CI, that happens when the code was checked out as one user and your script runs as another: a non-root image on the Docker executor, where the runner's helper container clones as root, or different user IDs for the helper and build containers on Kubernetes. GitLab Runner trusts the directory for its own checkout on the Docker and Kubernetes executors, but not for the Git commands in your script. Add git config --global --add safe.directory "$CI_PROJECT_DIR" to before_script, or make the users match. On shell runners, make the builds directory's owner match the user the jobs run as.

A Git command in a job stops before doing anything:

fatal: detected dubious ownership in repository at '/builds/group/project'
To add an exception for this directory, call:

	git config --global --add safe.directory /builds/group/project

Tools that run Git for you fail the same way. go build, for example, reports error obtaining VCS status: exit status 128.

Git won’t work in a repository owned by a different user than the one running it, so that a repository someone else planted can’t run commands through its config. In a CI job, that means the code was checked out as one user and your script runs as another. Here’s where that happens with GitLab Runner 19.5, and how to fix it.

Why the owners differ

Runner Checks out the code Runs your script
Docker executor The helper container, as root The job’s image: root, unless the image or runner sets another user
Kubernetes executor The helper container The build container, which can run as another user ID, as on OpenShift
Shell executor The runner’s user The same user

On the Docker and Kubernetes executors, the runner adds the project directory to safe.directory in its helper container before it fetches, so its own checkout works. Your script runs in the job’s image, which has its own Git config, so a non-root image still hits the error.

On the shell executor, the runner itself fails at Getting source from Git repository when jobs run as a different user from the one that owns the project’s folder, typically after jobs started running as root, for example with sudo gitlab-runner run, in folders that gitlab-runner created. A folder left owned by root while jobs run as gitlab-runner fails differently, with Permission denied or could not lock config file.

Fix: trust the project directory

default:
  before_script:
    - git config --global --add safe.directory "$CI_PROJECT_DIR"

Git then trusts that directory for the rest of the job. It reads safe.directory only from global and system config, and since Git 2.38 from the command line, never from the repository’s own, which is why the fix goes in --global. On shell runners, --global writes to the runner user’s own ~/.gitconfig, which adds the same line again on every job, so use the command-line form there.

If the image’s home directory isn’t writable, git config --global fails with could not lock config file. Give the image a writable HOME, or, with Git 2.38 or later, pass the setting to a single command:

git -c safe.directory="$CI_PROJECT_DIR" describe --tags

The project directory’s entry doesn’t cover submodules’ folders. git config --global --add safe.directory '*' trusts every repository, submodules included. In a container that exists only for one job, that’s usually acceptable; on a shell runner that many projects share, trust just the project directory.

Fix: make the users match

  • Docker executor: run the job as root with image:docker:user, in GitLab 16.8 and later, unless the runner’s config.toml sets its own user, which wins, or an allowed_users list without root. Or set FF_DISABLE_UMASK_FOR_DOCKER_EXECUTOR: "true" in the job’s variables: when the image’s own user isn’t root, the runner then gives the checkout to that user, which needs the id command in the image.

    test:
      image:
        name: my/non-root-image
        docker:
          user: root
  • Kubernetes executor: run the helper and build containers as the same user ID, with helper_container_security_context and build_container_security_context in the runner’s config.toml.

  • Shell executor: make the folder’s owner match the user the jobs run as: run the jobs as that user again, sudo chown -R the folder to the user they now run as, or delete the project’s folder so the runner clones it again. The runner’s safe_directory_checkout = true setting, in the [[runners]] section of config.toml, also adds the folder to safe.directory before each checkout. It’s on by default for the Docker, Kubernetes, VirtualBox and Parallels executors, and off for the shell, SSH, custom and instance executors.

On your phone

MergeBell shows a job’s log live, so the dubious ownership line is there as soon as Git prints it, and Retry reruns the job on the free tier. A retry hits the same error: the fix is a before_script line or a runner change. More Git trouble in CI is in GitLab CI shallow clones and GIT_DEPTH.

Frequently asked questions

What does "detected dubious ownership in repository" mean in GitLab CI?

Git won't work in a repository owned by another user, so that someone else's repository can't run commands through its config. In a CI job, the code was checked out as one user and the Git command runs as another, usually because the job's image runs as a non-root user while the runner's helper container cloned the code as root.

How do I fix dubious ownership in .gitlab-ci.yml?

Add git config --global --add safe.directory "$CI_PROJECT_DIR" to before_script, so Git trusts the project directory for the rest of the job. If the image's home directory isn't writable, that fails with could not lock config file: give the image a writable HOME, or, with Git 2.38 or later, pass the setting to a single command with git -c safe.directory="$CI_PROJECT_DIR".

Doesn't GitLab Runner set safe.directory already?

Only for its own checkout. On the Docker and Kubernetes executors, the runner adds the project directory to safe.directory in its helper container before it fetches the code. Your script runs in the job's image, with its own Git config, so its Git commands still need the setting when the users differ.

Is safe.directory '*' safe to use in GitLab CI?

It turns the check off for every repository. In a container that exists only for one job, that's usually acceptable. On a shell runner that many projects share, trust just the project directory instead.

Why does go build fail with "error obtaining VCS status: exit status 128" in GitLab CI?

Go runs Git to stamp version information into the binary, and Git stops with the dubious ownership error. Add safe.directory or make the users match, or build with -buildvcs=false if you don't need the stamp.

Sources

Checked against these pages on 16 November 2026. GitLab changes quickly — if something here no longer matches, tell us and we will fix it.

Do this from your phone with MergeBell

Free for the whole review loop: the To-Do inbox, diffs and line comments, approve and merge, pipelines with retry, and push alerts for your To-Dos. Works with gitlab.com and self-managed GitLab 16.0+.

Get MergeBell for iPhoneGet MergeBell for Android

On the App Store for iOS 26 or later and on Google Play for Android 12 or later. An independent app, not affiliated with GitLab Inc.