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 
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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?
@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.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.