1

I have a superproject where I have added a number of projects as submodules. These projects are part of a microservices architecture. The idea of using a superproject with (microservices) projects added as submodules is to be able to get all the projects in a single repository.

Another use case is to track the changes of all the projects. The idea was to branch the superproject, such that each branch would create a sort of a 'snapshot' of all the projects at a given time.

However, I found that whenever I create a new branch and update the submodules (git submodules update --remote), all the branches of the superproject gets updated. This is expected, as submodules are merely a link to the original project.

Now my question is, is there a strategy for me to take snapshots of a superproject? The way we can branch off a single project, is there such a way to do it for a submodule superproject?

Thank you.

madu
  • 5,232
  • 14
  • 56
  • 96
  • 1
    "*whenever I create a new branch and update the submodules (`git submodules update --remote`), all the branches of the superproject gets updated. This is expected*" Certainly not. AFAIU you don't sync submodules with the current state of the superproject with `git submodule update` when switching branches on superproject; you should. I do it in `post-checkout` hook. In more recent Git it's possible to configure to do that automatically. Or change branch with [`git checkout --recurse-submodules`](https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt---recurse-submodules). – phd Dec 26 '19 at 11:48
  • Thanks @phd Unfortunately that doesn't seem to work. I am not making any changes to the submodules in the superproject. I am using superproject as a collection of repositories and I want to use branches of the superproject as snapshots of the commits of all the projects at certain times. Whenever I create a new branch in superproject and do `submodule update --remote` or `submodule foreach git pull`, it updates all the previous superprojects branches to latest commits of the submodules. – madu Dec 27 '19 at 01:35
  • I have tried creating `orphan` branches and also setting branches as `--unset-upstream`, but still the superprojects branches keeps updating. – madu Dec 27 '19 at 01:38
  • 1
    `git submodule update` without `--remote` or `git checkout --recurse-submodules` update submodules to the commit remembered in the superproject. – phd Dec 27 '19 at 11:58

1 Answers1

1

I believe when that superproject was created you set the submodules to track a branch with the following

git submodule add -b <yourBranch> <URL>

This is why git submodule update --remote updates every submodule. So you might need the inverse of git submodule tracking latest.

One approach could be to start clean, remove the submodule, and re-add without the -b specifier.

To take a snapshot, create a new branch in the superproject, remove the submodule, and re-add without the -b specifier.

Create new branch

git checkout -b <branch-name>

Steps from how to remove a submodule:

1. git submodule deinit -f -- a/submodule    
2. rm -rf .git/modules/a/submodule
3. git rm -f a/submodule

Then add again without branch tracking information

git submodule add <URL>

After addition, there is the chance to checkout a specific commit in the submodule, therefore that will be tracked like a snapshot.

1. cd submodule
2. git checkout <commit hash>
3. cd ..

4. git add --all    # or after every submodule
5. git commit -m "Tracking revision xx"

6. git push

Steps 4 and 5 are not necessary after every submodule update, just recommended.

zerocukor287
  • 555
  • 2
  • 8
  • 23