I'm trying to check if the current repo is not a submodule but the top level
git repository. I have tried the following command: git submodule init
Output:
You need to run this command from the toplevel of the working tree.
But it will init the submodule. How can I check the current repository if it isn't a submodule but the top level git repository, without making a change in the repository?
You probably want git rev-parse --show-superproject-working-tree
, with a fallback to git rev-parse --show-toplevel
if you aren't in a submodule. For example:
toplevel=$(git rev-parse --show-toplevel)
superproject=$(git rev-parse --show-superproject-working-tree)
if [[ -z "$superproject" ]]; then
echo "submodule in $superproject"
else
echo "toplevel is $toplevel"
fi
I'm not currently aware of a builtin that provides an all-in-one answer for both situations.
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