# create sub a project cd ../test_sub_a_proj echo"# test_sub_a_proj" >> README.md git init git add README.md git commit -m "first commit for sub a" git remote add origin [email protected]:itabas016/test_sub_a_proj.git git push -u origin master
# create sub b project cd ../test_sub_b_proj echo"# test_sub_b_proj" >> README.md git init git add README.md git commit -m "first commit for sub b" git remote add origin [email protected]:itabas016/test_sub_b_proj.git git push -u origin master
# add the third repo for master git submodule add [email protected]:itabas016/test_sub_a_proj.git subA git submodule add [email protected]:itabas016/test_sub_b_proj.git subB git status # result: Changes to be committed: (use "git reset HEAD <file>..." to unstage)
new file: .gitmodules new file: subA new file: subB
# push the third submodule to master git commit -m 'thirdpart submodule A&B added.' git push -u origin master
# init sub repo git submodule init git submodule update
# another quick way to clone master with recursive git clone --recursive [email protected]:itabas016/test_master_proj.git
Update repo with submodule
normally, the repo update is used git pull, it will be resolve the conflict automatic. But when update the master repo, you should attent the sub repo whether has update, see the below:
# sometime should be run this command git submodule update --remote
# if submodule has other denpendency repo and has updated git submodule foreach "git submodule update"
# same as submodule master branch git submodule update --remote --rescursive
# if just update one submodule repo cd subA/ git pull
# if subA has denpendency with other repo git submodule foreach "git pull" --recursive
Modify repo with submodule
when execute git submodule update don’t checkout any branch, so the HEAD of submodule is detached state. when modify submodule repo, remember checkout related feature branch.
# if modify submodule repo before your change forgot to checkout remote master branch, # and have a commit in your local. Now we should use `cherry-pick` # switch submodule from detach state to remote master.
sed -i 's@subA@framework/subA@' .gitmodules sed -i 's@subA@framework/subA@' framework/subA/.git mv .git/modules/subA .git/modules/framework/subA sed -i 's@subA@framework/subA@' .git/modules/framework/subA/config