(Update April 2017 for Git 2.13, Q2 2017)
There is now an official command to determine if a repo is a submodule of a parent repo:
cd /path/to/potential/submodule/repo
git rev-parse --show-superproject-working-tree
See commit bf0231c (08 Mar 2017) by Stefan Beller (stefanbeller
).
(Merged by Junio C Hamano -- gitster
-- in commit 3edcc04, 17 Mar 2017)
rev-parse
: add --show-superproject-working-tree
In some situations it is useful to know if the given repository
is a submodule of another repository.
Add the flag --show-superproject-working-tree
to git-rev-parse
to make it easy to find out if there is a superproject.
When no superproject exists, the output will be empty.
Jethro Yu suggests in the comments:
get super project path regardless inside/outside of submodule:
git rev-parse --show-superproject-working-tree --show-toplevel | head -1
(Update 2014) As noted by Quentin Pradet, more recent Git submodule repos show a simple .git
file instead of a .git
folder.
That .git
file reference the path of the actual submodule git repo, stored in the parent repo .git/modules
subfolder.
jeffrson adds in the comments (2023):
Existing '.git
' itself is not sufficient, because that's the same for worktrees.
However, the file should contain the string "modules
" or "worktrees
", respectively, as part of the target.
(Original answer: Sept. 2011)
The very nature of a submodule is for the git repo acting as submodule has no idea it is used as a submodule by a parent repo.
One dirty trick would be to:
- change a file
- go back one level above the current repo
- try a "
git status --ignore-submodules=none
"
- restore the changed file.
If you see the file in the result of the git status
, your repo should be a submodule.
If it is only a nested repo, the git status
should ignore your nested repo entirely.