Using $GIT_DIR in pre-commit hook would return the location of the .git directory. Even if it had not been explicitly set to anything, using it in the pre-commit hook would still return you the location. After an update, moving to Git 2.18 this appears to no longer be the case and the behavior of my pre-commit hooks are different and not working as intended. Any idea how to fix this?
2 Answers
Some really detailed information on this can be found here
https://public-inbox.org/git/20180826004150.GA31168@sigill.intra.peff.net/t/
I will attempt to paraphrase to the best of my ability for the sake of question quality.
Using $GIT_DIR, when it has not been explicity set, in pre-commit hooks did work pre Git 2.18, however this was an unexpected side effect and not intended behavior.
A change in GIT 2.18 caused this to stop working. In the link a contributor mentions that the correct way to get the location of the .git directory in a pre-commit hook (or any hook for that matter) is to use this git command
git rev-parse --git-dir
They may, in future, produce a patch to return the behavior to how it was pre 2.18 but I would recommend not relying on undocumented and unintended behavior

- 2,829
- 3
- 17
- 44
-
1With the addition of worktrees in git 2.5, `--git-common-dir` is a more reliable way to find the functional `.git` directory. The unfortunate bit is you have to do [hacks like this](https://github.com/pre-commit/pre-commit/blob/76e0f11916e1376b646126b90d00b6f73dbdd0cf/pre_commit/git.py#L33-L40) to support both versions – anthony sottile Nov 06 '18 at 04:38
The fix is, put
export GIT_DIR=${GIT_DIR-`git rev-parse --git-dir`}
up front in your hook.

- 55,082
- 5
- 77
- 137