Want to nest one repo inside another and pretend everything is tidy while actually juggling two histories Well welcome to the glamorous world of git submodules This guide walks through adding a submodule to a GitHub repository and keeping the pointer tidy so your team does not cry
Make sure your working tree is clean and decide which repository will live inside the main repo and where it will sit in the file tree The main repo will only store a pointer to a specific commit in the subproject so history stays separate and manageable
Run the add command which creates a .gitmodules file and a pointer in the main tree
git submodule add REPO_URL PATH
That creates a .gitmodules entry and places a gitlink in PATH which points to a commit in the subproject
Stage the new files and push the pointer to origin so other clones know about the submodule
git add .gitmodules PATH
git commit -m "Add submodule"
git push
When someone clones your repo they need to fetch submodule data as well The quickest safe sequence is
git clone REPO_URL
git submodule update --init --recursive
If the repo is already cloned and you pulled new commits with submodule changes run
git submodule update --init --recursive
The submodule is a separate git repo inside your working tree If you need to change what commit is referenced enter the submodule do your fetch or checkout then update the main repo pointer
cd PATH
git fetch
git checkout BRANCH_OR_COMMIT
cd ..
git add PATH
git commit -m "Update submodule"
git push
Note that by default submodules can end up in a detached HEAD state If you want the submodule to track a branch use the set branch command or update .gitmodules
git submodule set-branch --branch BRANCH PATH
Removal needs a few steps to clean up the .gitmodules entry and repo metadata
git config -f .gitmodules --remove-section submodule.PATH
git rm --cached PATH
rm -rf .git/modules/PATH
git commit -m "Remove submodule"
git push
There you go You added a submodule you committed the pointer you taught the team how to clone and update and you learned how to remove it without leaving ghost metadata behind Your main repo stays slim while the subproject keeps its history neat and separate That is the whole point and sadly the only neat thing about git sometimes
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.