$GIT_DIR no longer set in pre-commit hooks

Viewed 1614

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

The fix is, put

export GIT_DIR=${GIT_DIR-`git rev-parse --git-dir`}

up front in your hook.

Related