Set up: This allows the repository to be local a file directory:
|
ROOT=/tmp echo $ROOT git config --global protocol.file.allow always |
Create remote:
|
cd $ROOT mkdir remote git init --bare remote |
Creating repos1
|
mkdir repos1 cd $ROOT/repos1 git init touch main.cpp git add . git commit -m 'main.cpp added' git remote add origin ../remote/ git push --set-upstream origin master git push origin master |
Creating lib1
|
cd $ROOT mkdir lib1 cd $ROOT/lib1 git init . touch vision.hpp git add . git commit -m 'vision.hpp added' # this alows to push into this repository, as it is a non-bare repository git config --local receive.denyCurrentBranch updateInstead |
Adding lib1 as submodule to repos1
|
cd $ROOT/repos1 git submodule add ../lib1/ git diff --cached lib1/ # This command lists the submodules recorded in the .gitmodules file along with their current checked out commit. git submodule # Gives you the commit from repose lib1 that has been added git ls-tree master git add . git commit -m 'lib1 as submodule added' git push origin master |
Cloning a Project with Submodules to repos2
|
cd $ROOT mkdir $ROOT/repos2 cd $ROOT/repos2 git clone $ROOT/remote/ . git submodule init git submodule update # Simpler way: # git clone --recurse-submodules ../remote/ . #If you already cloned the project and forgot --recurse-submodules, you can combine the git submodule init and git submodule update # We are in Detached HEAD State cd $ROOT/repos2/lib1/ git checkout master git branch -vv |
Working on Submodule lib1 in repos2:
|
cd $ROOT/repos2/lib1/ touch reader.hpp git add . git commit -m 'reader added from repos2' cd ../ git add . git commit -m 'submodule lib1 updated' git push |
Pulling in Upstream Changes from the Submodule Remote in repos1:
|
cd $ROOT/repos1/lib1 git fetch git merge now cd ../ git diff --submodule # There is an easier way to do this git submodule update --remote |