daniel451
daniel451

Reputation: 11002

Git repo with submodules does not track local changes?

I have a git repository with 3 submodules, like this:

foo/  # main repository
  bar1/  # submodule1
  bar2/  # submodule2
  bar3/  # submodule3

I've added the submodules directly after executing git init in the main repository:

git submodule add https://github.com/bar1.git bar1
...

The strange thing now is if I add a new directory like foo/test/ git does not track changes inside of foo/test/. Just changes directly inside of foo/ are tracked or changes to the submodules. Why is that?

It appears to me like git is treating foo/test like a submodule, while it is certainly not.

The .gitignore looks like this:

.idea/

...which basically only ignores the hidden IDE project-related dir.

.gitmodules looks like this:

[submodule "bar1"]
path = bar1
url = https://github.com/...

[submodule "bar2"] path = bar2 url = https://github.com/... [submodule "bar3"] path = bar3 url = https://github.com/...

Do I have to manually tell git that foo/test/ should remain inside of the main repository and that it is not a submodule?

Upvotes: 2

Views: 2760

Answers (2)

daniel451
daniel451

Reputation: 11002

I've just found the problem. I do not know why, but my git-config was messed up.

Doing a simple git config --list (inside of foo/) revealed that core.worktree was wrong:

core.worktree=../../../bar1

I can not remember altering the config manually. I've just cloned a fresh copy of my repository, updated the submodules and copied my new changes foo/test/ into the freshly cloned repo. Now everything works.

I guess the only question is how I could have messed up the git config of the "old/bugged" repo by mistake...

Upvotes: 1

gabe3886
gabe3886

Reputation: 4265

in the foo folder, you'll have a .git folder, which is the git repository for for foo. With submodules, when you do git submodule add bar1 it creates a .git folder in bar1 when it checks out.

When you commit the changes in foo, including the submodules, git knows (I don't know the workings to explain how) that there's a .git folder within bar1, and therefore it is a repository of its own. If that is registered as a submodule in the .gitmodules file, then it will get the most recent hash of the submodule and commit that in place of the folder (you'll be able to see it in Github or whatever manager you use if you click the folder, it will load a file with the hash). This is how the git submodule init and git submodule update commands know which point to check out to.

Any other folder which isn't a submodule (and therefore doesn't contain a .git folder within it) it treated as part of the parent repository, in this case foo. You do not have to tell git that foo/test is part of the main repository, it knows it is already.

Upvotes: 1

Related Questions