pre-commit never installs from the repository under test, only the configuration (otherwise caching is intractable)

the working directory during installation is implementation detail and not customizable, it is the root of the hook repository itself inside the pre-commit cache

for things like pylint which need dynamic analysis and direct access to your codebase and dependencies an unmanaged repo: local hook is suggested instead (or enumerate your dependencies in additional_dependencies


disclaimer: I created pre-commit

Answer from anthony sottile on Stack Overflow
🌐
pre-commit
pre-commit.com
pre-commit
pre-commit will automatically mount the repository source as a volume using -v $PWD:/src:rw,Z and set the working directory using --workdir /src.
Discussions

In a git hook is the current working directory guaranteed to be within the git repository? - Stack Overflow
A bare repo has no working directory hence no "repository root". Also it will break when .git is a link instead. 2015-09-14T05:06:13.377Z+00:00 ... Figured. Any way to get the repo root under normal usage? For a pre-commit hook which uses the repo as configuration files for a program, for instance? More on stackoverflow.com
🌐 stackoverflow.com
Question: How to change working directory in `system` language hook
A framework for managing and maintaining multi-language pre-commit hooks. - pre-commit/pre-commit More on github.com
🌐 github.com
6
April 3, 2019
Git pre-commit hooks per directory - Stack Overflow
How do I format a .pre-commit-config.yaml so it's different depending on my directory? My project has different directories. One directory is a UI directory with a package.json and Django files. A... More on stackoverflow.com
🌐 stackoverflow.com
Setting up pre-commit hooks to existing and all future repo's (check files for text before allowing commit)
One way to do that is by setting up a Git template, as described in man git-init. You can create a template directory somewhere (e.g. ~/.config/git/template) and set init.templateDir to that path. In there, create a directory called hooks and put the pre-commit script in there (it must be executable). If you now create new repositories, your template, and thus your hooks as well, will be copied into the .git directory. To "re-initialize" existing repos with the new hooks, you can simply run git init in the repository. Note that latter is only possible once - Git will not overwrite any files that already exist. A second variant, which I personally use, are global hooks. Those won't be copied to the repositories, but they will be used by all repositories. To use that method, you again have to create a hooks directory (e.g. ~/.config/git/hooks) and then point core.hooksPath to that directory. Then simply put your hook there and you're done. I'm using a more sophisticated hooks system, which will run both global and local hooks, etc. If you're interested, feel free to look at my configs: ~/.gitconfig and ~/.config/git/ . More on reddit.com
🌐 r/git
2
5
December 12, 2018
🌐
GitHub
github.com › pre-commit › pre-commit › issues › 1110
How to run pre-commit inside folder · Issue #1110 · pre-commit/pre-commit
August 6, 2019 - . ├── README.md ├── backend │ ├── Pipfile │ ├── Pipfile.lock │ ├── mypy.ini │ ├── pylintrc │ ├── pyproject.toml │ ├── src │ ├── tests │ └── tox.ini ├── docker │ ├── backend │ └── postgres ├── docker-compose.yml └── .pre-commit-config.yaml
Author   pre-commit
🌐
Git
git-scm.com › docs › githooks
Git - githooks Documentation
Before Git invokes a hook, it changes its working directory to either $GIT_DIR in a bare repository or the root of the working tree in a non-bare repository. An exception are hooks triggered during a push (pre-receive, update, post-receive, post-update, push-to-checkout) which are always executed in $GIT_DIR.
🌐
Stefanie Molin
stefaniemolin.com › articles › devx › pre-commit › behind-the-scenes
A Behind-the-Scenes Look at How Pre-Commit Works | Stefanie Molin
May 4, 2025 - This is why every collaborator on your project must run pre-commit install locally: pre-commit needs to install its executable at .git/hooks/pre-commit, but that file (and everything in the .git/hooks/ directory) is not part of Git's version control, and therefore, only exists locally on each collaborator's machine.
🌐
GitHub
github.com › pre-commit › pre-commit › issues › 989
How to change working directory in `system` language ...
April 3, 2019 - Hello, I want to run python manage.py from system hook, but manage.py is in another directory.
Author   pre-commit
Find elsewhere
🌐
Medium
medium.com › @tahseen.adit › understanding-pre-commit-an-under-the-hood-look-218284e7b9d9
Understanding ‘pre-commit’: An Under-the-Hood Look | by Md Tahseen Anam | Medium
May 30, 2024 - For each hook, pre-commit creates an isolated environment (e.g., a virtual environment for Python hooks, or a Node.js environment for JavaScript hooks). You can see it if you go to the repo directory in the .cache/pre-commit directory.
🌐
GitKraken
help.gitkraken.com › gitkraken-client › githooksexample
Pre-Commit Hook Example in GitKraken Client | Git Hooks Example
June 28, 2024 - Now that we have our pre-commit file, we need to make it executable. To do this we will need the command line. Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to .git/hooks.
🌐
Reddit
reddit.com › r/gitlab › pre-commit hooks for specific directories in a mono repo
r/gitlab on Reddit: Pre-commit hooks for specific directories in a mono repo
March 7, 2023 - The example is this the right path for git pre-commit configs? repos: - repo: https://github.com/pre-commit/pre-commit-hooks/apps/{ Directory }
🌐
Stack Overflow
stackoverflow.com › questions › 61291054 › git-pre-commit-hooks-per-directory
Git pre-commit hooks per directory - Stack Overflow
You can just execute commands in bash/other and change directory yourself! That's what worked best for me. ... - repo: local hooks: - id: go-unit-tests name: run go test s(go test) language: system entry: bash -c 'cd subdir && exec go test ./...' pass_filenames: false types: [go] files: ^subdir/ ... - repo: local hooks: - id: pytest name: pytest entry: sh -c 'cd text2sql-backend && PYTHONPATH=. poetry run pytest .' language: system types: [python] pass_filenames: false stages: [commit] exclude: ".*pb2(_grpc)?.py(i)?$|__init__.py"
🌐
Git
git-scm.com › book › en › v2 › Customizing-Git-Git-Hooks
Git - Git Hooks
There are a lot of client-side hooks. This section splits them into committing-workflow hooks, email-workflow scripts, and everything else. The first four hooks have to do with the committing process. The pre-commit hook is run first, before you even type in a commit message.
🌐
Research Software Engineering Sheffield
rse.shef.ac.uk › blog › pre-commit
Research Software Engineering Sheffield - pre-commit : Protecting your future self
October 10, 2022 - It is however useful to enable running pre-commit as part of your Continuous Integration/Development pipeline (CI/CD). This can be done with both GitLab and GitHub although similar methods are available for many continuous integration systems. GitHub actions reside in the .github/workflows/ directory of your project.
🌐
Reddit
reddit.com › r/git › setting up pre-commit hooks to existing and all future repo's (check files for text before allowing commit)
r/git on Reddit: Setting up pre-commit hooks to existing and all future repo's (check files for text before allowing commit)
December 12, 2018 -

I'm looking to install some pre-commit hooks locally for existing and future repo's I use. Specifically, I just want to scan files for some text and if it's found not let me commit anything (e.g. password=(.+)).

I went through a few different install steps, but nothing seems to work. Updating my gitconfig to link to a common hooks directory, etc.

How the heck do I do this?

As an aside - anyone able to recommend some "idiot proof" hooks?

🌐
The Dissonance
thedissonance.net › 2024 › 03 › 27 › pre-commit-hooks.html
Maintaining pre-commit hooks across repositories | The Dissonance
March 27, 2024 - This setup is versatile enough that I don’t expect to outgrow it for a long time. One limitation is that doesn’t support versioning of the different variations of hooks. The central ~/dev/hooks/pre-commit directory can be versioned of course.
🌐
Poetry
python-poetry.org › docs › 1.7 › pre-commit-hooks
pre-commit hooks | 1.7 | Documentation | Poetry - Python dependency management and packaging made easy
The default arguments are args: ["-f", "requirements.txt", "-o", "requirements.txt"], which will create/update the requirements.txt file in the current working directory. You may add verbose: true in your .pre-commit-config.yaml in order to output to the console:
🌐
Medium
medium.com › data-science › custom-pre-commit-hooks-for-safer-code-changes-d8b8aa1b2ebb
Custom pre-commit hooks for safer code changes | by Thierry Jean | TDS Archive | Medium
March 14, 2024 - Ideally, you’d want to avoid adding a commit to trigger the hook each time you want to test changes. The pre-commit library provides utilities to facilitate this process, but it requires a few manual steps detailed in pre-commit GitHub issues. Go to your directory /my-project where you’d like to test your hook.
🌐
Super User
superuser.com › questions › 1581253 › getting-current-working-dir-in-git-hook
Getting current working dir in git hook - Super User
Are fine with committing files under /projects/projectx/backend/ if the git commit is done from /projects? Your scenario sound weird to me, but it may be legit - please enlighten me. ... This isn't possible with Git. Git intentionally changes into a consistent directory when running hooks to make it easy to write hooks and doesn't expose the original working directory.
🌐
Medium
medium.com › @0xmatriksh › how-to-setup-git-hooks-pre-commit-commit-msg-in-my-project-11aaec139536
How to setup git hooks(pre-commit, commit-msg) in my project? | by Kishmat | Medium
August 4, 2023 - After setting up and your projects with some code in .py file/s. We can setup the pre-commit and actually run it before committing the changes. First, we .git directory in your project directory Make sure you have initialize the git with git init and you have .git folder in your repository.
🌐
Poetry
python-poetry.org › docs › pre-commit-hooks
pre-commit hooks | Documentation | Poetry - Python dependency management and packaging made easy
The default arguments are args: ["-f", "requirements.txt", "-o", "requirements.txt"], which will create/update the requirements.txt file in the current working directory. You may add verbose: true in your .pre-commit-config.yaml in order to output to the console: