Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a git hook is the current working directory guaranteed to be within the git repository?

Tags:

git

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?

like image 334
Dickon Reed Avatar asked Aug 15 '11 13:08

Dickon Reed


People also ask

What directory do Git hooks run in?

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.

What is current directory in Git?

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.

Are Git hooks stored in repo?

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.

What does Git hook do?

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.


1 Answers

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

like image 186
Ohad Schneider Avatar answered Oct 13 '22 04:10

Ohad Schneider