Reputation: 1354
I read some info about submodules, here, here and here .
My question that remains is: Can I simply track my submodule's master branch in my main project's master branch and my submodule's production branch in my main project's production branch? Is there an easy way to do that?
Thank you!
Upvotes: 1
Views: 63
Reputation: 638
You can use git hooks. Before/After certain git
actions, git can run scripts and take action. The hook you are probably most interested in is the post-checkout
hook.
Basically, within your repository is a .git/hooks
directory. If you have executable permissions on a file and the file exactly matches the name of an event (ie: no .sample
), then git
will execute the script at the proper time.
vim .git/hooks/post-checkout
chmod +x .git/hooks/post-checkout
Using this information you could create a bash script that checks out each of your submodules after checking out a branch in the main repository:
#!/bin/bash
CURRENT_GIT_BRANCH="`git rev-parse --abbrev-ref HEAD`"
cd modules/module001
git checkout ${CURRENT_GIT_BRANCH}
cd ../../
cd modules/module002
git checkout ${CURRENT_GIT_BRANCH}
You could even get fancy and use a list of modules by looking at the directory structure, or the .gitmodules
file of your repository.
This means that when you perform git checkout BRANCH
in your main repository, the same branch will be checked out for each submodule listed and described in the post-checkout
hook.
It should be noted that you cannot commit the hooks to your repository directly, as that could pose security risk when cloning new repositories, etc.
Upvotes: 2