Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the current repository is the top level git repo

Tags:

git

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?

like image 590
TTaJTa4 Avatar asked Sep 02 '25 03:09

TTaJTa4


1 Answers

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.

like image 170
Todd A. Jacobs Avatar answered Sep 04 '25 17:09

Todd A. Jacobs