Fixing FatalError: git failed. When Running pre-commit in GitLab Pipeline

Опубликовано: 27 Март 2026
на канале: vlogommentary
3
like

Learn how to resolve the 'FatalError: git failed.' issue when running pre-commit hooks in a GitLab CI pipeline, including the key step of configuring git safe directories.
---
This video is based on the question https://stackoverflow.com/q/79391640/ asked by the user 'mdailey77' ( https://stackoverflow.com/u/2026659/ ) and on the answer https://stackoverflow.com/a/79403367/ provided by the user 'mdailey77' ( https://stackoverflow.com/u/2026659/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Getting `An error has occurred: FatalError: git failed.` when running pre-commit in GitLab pipeline

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.

If anything seems off to you, please feel free to drop me a comment under this video.
---
The Problem: pre-commit Fails in GitLab CI with a Git Error

When running pre-commit in a GitLab pipeline with a custom Python image, you might encounter this error:

[[See Video to Reveal this Text or Code Snippet]]

This occurs despite git being installed and the pipeline clearly running inside a Git repository.



Understanding the Issue

pre-commit relies on Git to identify the repository state.

The error usually indicates that though git exists, it cannot operate due to permissions or repository trust issues.

Git 2.35+ introduced a security feature requiring repositories to be explicitly trusted (safe.directory) when running Git commands in CI or different user contexts.

If Git doesn’t recognize the repository as safe, commands invoked by pre-commit will fail.



Concise Solution: Configure Git Safe Directory

Before running your pre-commit checks in the pipeline, add this command:

[[See Video to Reveal this Text or Code Snippet]]

This marks all directories as safe, avoiding Git safety checks that block operations in CI environments.

Updated GitLab CI Job Example

[[See Video to Reveal this Text or Code Snippet]]



Additional Tips

Always verify git version and pre-commit are installed correctly in your custom image.

Ensure your pipeline job checks out the code (GitLab does this by default).

Keeping the safe.directory config ensures Git commands in non-standard environments run without permission issues.



Summary

If you see FatalError: git failed. running pre-commit in GitLab CI, it’s usually a Git trust issue. Setting:

[[See Video to Reveal this Text or Code Snippet]]

in your pipeline before invoking pre-commit will solve the problem and let your hooks run smoothly.