12

We've been looking into git submodules and we are wondering what advantage (if any) is there in having a repository using submodules verses having a repository within another repository with a .gitignore file.

Example without submodules:

mkdir a
cd a
git init
touch test1.txt
echo "b" > .gitignore
git add .
git commit -m "Adding test1.txt and gitignore"

mkdir b
cd b
git init
touch test2.txt
git add .
git commit -m "Adding test2.txt"

git log
cd ..
git log
Dooltaz
  • 2,413
  • 1
  • 17
  • 16

1 Answers1

20

The git parent (of the submodules) will keep track of the branches and tag IDs of the submodules when you commit. That will ensure that when you check out the parent (at a known version) then the submodules will also contain their right tags.

If, as above, it just happens to be an ignored subdirectory, then basically these are two independent git repos, as if they'd not been part of a file system hierarchy.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • 2
    This is exactly the key differentiator: using submodules you have a versioned object in the parent repository that represents the submodule. Nested (and ignored) sub-repositories have no representation at all in the parent repo. – Dan Moulding Sep 23 '09 at 01:50
  • Hi, I'm having a problem understanding this: let's say the working repo is A and the submodule repo is b. Do you mean that b keeps track of changes made to its code within A? Could you please provide an example? – Aditya M P Aug 09 '12 at 03:18
  • @adityamenon I've viewed it as an organized way of being able to do `git submodule foreach git pull` without remembering where my "submodules" are. – JRomero Aug 09 '12 at 03:49
  • 1
    @AdityaMP let's say you have repo A as a web app. A has a backend B and a frontend F, and they work together. Now, let's assume B has 3 versions, and F has 2: B1 works with F1, B2 and B3 works with F2, and B4 works with F3. If it were just folders, a single commit does not guarantee compatible versions, but if we are using submodules, then each commit can link compatible versions. – Smily Sep 17 '20 at 22:17