Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git command line - know if in submodule?

Tags:

Is there any way in git to know if you're in a submodule? You can do thinks like git submodule foreach in the parent directory but I can't seem to come up with a generic way to show that you're in a submodule if you're in one, or in any of the child directories inside the submodule.

I guess you could find the repo root with git rev-parse --show-toplevel, and then cd-ing up a level, and finding the root of that repo again and then comparing the list of submodules to the current directory, but that seems so sticky...

like image 973
Andy Ray Avatar asked Sep 09 '11 08:09

Andy Ray


People also ask

How do I know my submodule?

You can use git submodule status or optionally git submodule status --recursive if you want to show nested submodules. From the Git documentation: Show the status of the submodules.

What is git submodule command?

Git submodules allow you to keep a git repository as a subdirectory of another git repository. Git submodules are simply a reference to another repository at a particular snapshot in time. Git submodules enable a Git repository to incorporate and track version history of external code.

Where can I find Gitmodules?

gitmodules file, located in the top-level directory of a Git working tree, is a text file with a syntax matching the requirements of git-config[1]. The file contains one subsection per submodule, and the subsection value is the name of the submodule.

Does git pull include submodules?

Pulling with submodules. Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .


1 Answers

(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.


(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.

like image 131
VonC Avatar answered Oct 01 '22 12:10

VonC