Experimentally it seems that git hooks get run with the current directory set to be the root of the repository. However, I can't see any guarantee about that in the git documentation. Should I rely on the current working directory to locate the git repository, or is there a better way to work out the git repository associated with the hook?
By default the hooks directory is $GIT_DIR/hooks , but that can be changed via the core. hooksPath configuration variable (see git-config[1]). 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.
git directory is a configuration file for git. Use the terminal to display the . git directory with the command ls -a . The ls command lists the current directory contents and by default will not show hidden files.
By default hooks are stored in . git/hooks outside of the working tree and are thus not shared between users of the repository. The hooks can be included in a directory within the repository and then each developer can set Git up to use them.
Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git's internal behavior and trigger customizable actions at key points in the development life cycle.
The current answers appear to be outdated. As of 2.9.0, the docs state the following:
Before Git invokes a hook, it changes its working directory to either the root of the working tree in a non-bare repository, or to the $GIT_DIR in a bare repository.
https://git-scm.com/docs/githooks/2.9.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With