Reputation: 331
I have a project Alpha that has a dependency on another project, DM. Project DM represents the data model and is used by other projects as well.
My team decided to put project DM on its own git repository to avoid duplicate/diverging code, and to include it in project Alpha's git repo as a submodule.
When I modify file1.txt
corresponding to project DM, located in /home/me/projects/Alpha/DM/file1.txt
, running
/home/me/projects/Alpha $ git status
yields: "modified: DM (new commits)
".
If I understand well, doing $ git add DM
and $ git commit -m "Blo bli"
updates the DM commit that project Alpha's commit points to (so, a given project Alpha commit knows what version of DM it is compatible with).
So, does that mean that (after pushing my changes) if someone from my team working on project Alpha runs /home/teammate/my-projects/Alpha $ git pull
, then his project Alpha's files will be updated to those of the latest commit AND the files under /home/teammate/my-projects/Alpha/DM/
will be updated to the version corresponding to that commit as well? Does he need to run any additional commands to update the submodules to the correct version?
Thanks.
Upvotes: 0
Views: 110
Reputation: 1486
You will only have a new commit in project DM at this stage. In project Alpha (and also in the other project) now will be the submodule changed and with the next commit of Alpha the referece to the submodule change can be committed. Thus: you have to do an additional commit in each repo which references the submodule.
As you want to checkout old commits from Alpha with the old commit of DM, the double commit is required.
Some additional info on submodules you'll find at https://chrisjean.com/git-submodules-adding-using-removing-and-updating/
Upvotes: 0
Reputation: 311606
First, an opinion: you will find life much simpler if you treat submodules as primarily read-only, and only ever update them by pulling in changes from the upstream remote.
If I understand well, doing $ git add DM and $ git commit -m "Blo bli" updates the DM commit that project Alpha's commit points to (so, a given project Alpha commit knows what version of DM it is compatible with).
The answer is "sort of". A checked out submodule may be in "detached head" state, in which case adding and committing files won't do anything useful (this is partly why I suggest not editing files in a submodule repository). Assuming that your local submodule is on a named branch, then to commit a change that will be visible to consumers of the parent (Alpha) project, you will first do what you described:
cd DM
git add my-changes
git commit -m 'some changes'
git push
And then commit in the parent repository to record the new commit of the submodule:
cd ..
git add DM
git commit -m 'updated DM submodule'
git push
After this, new clones of the Alpha repository will see your updated changes. Existing checkouts will need to run:
git pull
git submodule update
Upvotes: 1