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

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

It is based on the value set for environment variable GIT_DIR. It is set to the root of the repository when the hook starts running. Many hooks, especially those doing a pull from another repo, unset ( and reset) this environment variable as needed.

Solution 3 - Git

You can use the environment variable $GIT_DIR. $GIT_DIR points at the .git directory.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDickon ReedView Question on Stackoverflow
Solution 1 - GitOhad SchneiderView Answer on Stackoverflow
Solution 2 - GitmanojldsView Answer on Stackoverflow
Solution 3 - GitholygeekView Answer on Stackoverflow