Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `git submodule` require fetching from a remote repository everytime?

Judging just from the number of results for "git submodule" here on SO alone, this is clearly a commonly asked and easily confused topic, so I will try to be as precise as possible.

Forgetting everything about updating/committing/branching submodules (which I understand greatly complicates things), why do submodules get emptied each time I change branches? From my current understanding, this makes branches expensive; what if I'm at the airport, and can't easily/cheaply connect? Am I doing something wrong, or there's some development philosophy that I'm not yet aware of?

Examples never hurt:

## make a new project
$> git --version
git version 1.7.5.4
$> mkdir new_proj; cd new_proj; git init
$> touch new_file_1.txt; touch new_file_2.txt
$> git add . && git commit -m "first commit"

## move into some development branch
$> git checkout -b cool_feature
$> <hack hack hack>
# in the middle, I add a submodule
$> git submodule add https://github.com/some/other_proj.git other_proj
$> git submodule update --init
$> ls -lR
new_file_1.txt
new_file_2.txt
other_proj
other_proj/that_file
other_proj/another_file

## I have to go back to master to do some work
$> git checkout master
# Why is other_proj still around?
$> git status
Untracked: other_proj
## Fine, I'll remove it, since I want a clean working copy, because I need to do some work and commits
$> git clean -f -d
$> <work work work>

## Now I'm ready to go back to cool_feature, but my submodules are empty!
$> git checkout cool_feature

At this point, I'm supposed to git submodule update, but what if I can't/it's expensive (e.g. it's remote, and I don't have internet access/it's slow).

The best workaround I've come up with is to clone all the submodules that I care about into a completely separate location, and then submodule from my local clones; this preserves the cheapness of submodules. Of course, this adds another layer of complexity when you're working on a team. :/

like image 407
Matt Avatar asked Oct 19 '11 02:10

Matt


People also ask

Is using git submodules a good idea?

In most cases, Git submodules are used when your project becomes more complex, and while your project depends on the main Git repository, you might want to keep their change history separate. Using the above as an example, the Room repository depends on the House repository, but they operate separately.

Do submodules update automatically?

Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated. When adding a submodule to a repository a new . gitmodules file will be created.

Does git pull pull 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 .

Does git fetch update submodules?

If you run git submodule update --remote , Git will go into your submodules and fetch and update for you.


1 Answers

Considering a submodule is nothing more than a pointer to a commit of another repo, the git submodule update is a bit unavoidable (in order to get back the content associated with said pointer).

One other workaround would be to clone your main repo:

  • one for the cool_feature branch, where you have your submodules
  • one for the master branch, where you don't have any submodules

Switching from one branch to another wouldn't require a git checkout (and its associated git submodule update), but a change of path.

The other workaround, if you want to work in one directory, has been described in "Replaced third party code with git submodules, now I can't switch branches"

move the submodule directory out of the way before switching to the master branch

like image 109
VonC Avatar answered Nov 15 '22 06:11

VonC