简体   繁体   中英

Is a Git submodule an independent repository?

When I look at my Git repository I can see that there is a .git folder inside the main repository. Then, when I goto into the submodule, I can see there is no folder called .git but there is a file called .git. Why this difference?

If the submodule is an actual repository like the main repository and can be worked on basically independent of the main repository, shouldn't both have a .git folder or both have .git file and not be different like this?

The git storage directory for a submodule is stored within the .git/ directory of the main repository.

You can find them under .git/modules/ :

ls .git/modules

Yes, every submodule is its own separate repository.

There was a period in early git submodule when it was like this: both the superproject and an every submodules have their own .git folders. Later it was decided that all git -related files should be stored under one .git directory even for submodules. So git submodule grew a subcommand git submodule absorbgitdirs that moves .git directories for submodules to the superproject's .git/modules/ and leave a file .git that points to the superproject's .git/modules/ .

git clone --recursive does that (move to superproject's .git/modules/ ) automatically.

Yes, every Git submodule is an independent repository. What you're seeing is called a gitlink, and it's a way of making the .git contents point somewhere else. The original idea was to use symlinks for this, but Windows requires elevated privileges or special settings to use symlinks, so Git had to use gitlinks instead.

The reason that you want to use gitlinks for submodules is because it makes it easier to deinitialize a submodule (that is, get rid of its contents) without blowing away its history. If you have a gitlink, then as long as the working tree of the submodule is clean, deinitializing it is essentially just an rm -fr on the directory, which is very convenient. Moreover, because people do exactly that sometimes with submodules, that means they haven't blown away their history unexpectedly.

It is still possible to use a separate .git directory if you want, but there's usually no good reason to do so unless for some reason your submodule is on a separate mount point.

If you want to find the actual Git directory of a repository, do this: git rev-parse --git-dir or git rev-parse --absolute-git-dir .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM