Is it possible to programmatically find the path to the .git directory including for submodules?
I'm trying to write a script that installs a git hook but I can't find a straightforward way to find the .git directory for the project. Ideally it should cover the following cases:
I could do all of this manually but I'd rather not parse the .git file of a submodule.
Edit: I'd be interested in a solution in python as well.
git rev-parse --git-dir
seems to be what you want. I just tested it with a submodule:
cd mymodule
git rev-parse --git-dir # --> .git
git submodule add ssh://.../path/to/mysubmodule
cd mysubmodule
git rev-parse --git-dir # --> /home/.../mymodule/.git/modules/mysubmodule
This surprised me, since there was a .git
in mysubmodule
, but it turned out to be a file, with the following contents:
gitdir: ../.git/modules/mysubmodule
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