If you need to nest one repository inside another without turning your project into version control quicksand you are in the right place. This guide explains how to use git submodule add to manage dependencies like a civilized developer. Expect a little bureaucracy and fewer surprises.
A git submodule links a parent repository to a specific commit in a child repository. The parent keeps a pointer and a small file named .gitmodules that records the remote URL and path. Think of it as a sticky note that says which version of the other repo to use. This keeps dependencies explicit and prevents accidental updates from sneaking in during code review.
Before you invite a submodule to live in your repo make sure the parent repo is healthy and the working tree is clean. Run git status and fix any loose changes. Pick the folder path where the nested repo will live and confirm you can reach the remote repository with your SSH key or HTTPS credentials.
The actual command is mercilessly simple. From the parent repository run the add command with the remote URL and the folder path where you want the new repo to appear.
git submodule add REPO_URL PATH
This creates a .gitmodules file and a new folder that behaves like a nested repository. Yes this is like Russian dolls for developers. Yes it can be useful when done right.
Stage and commit the new references so your teammates do not get lost.
git add .gitmodules PATH
git commit -m "Add submodule"
git push
If you are cloning a parent repository that already contains submodules use this to fetch everything in one pass.
git clone --recurse-submodules REPO
If you already cloned the repo run this to initialize and update nested content.
git submodule update --init --recursive
When the nested repository needs to move forward you work inside it and then update the parent so it points at the new commit. Do not try to push a vague wish into the parent and hope for the best.
cd PATH
git fetch
git checkout BRANCH
Then return to the parent repo and record the updated pointer.
cd ..
git add PATH
git commit -m "Update submodule"
git push
From the parent repo you can run an update to sync the checked out commits for all submodules.
git submodule update --remote --merge
Use git submodule add when you want explicit control over nested repositories. Remember to keep the parent working tree clean before adding a submodule. Commit .gitmodules and the submodule folder. Clone with --recurse-submodules or run update --init --recursive after cloning. When you change the submodule work inside it then update the parent reference and commit. Follow these steps and your developer workflow will run smoother with fewer mysterious missing files.
Final tip Keep a short README in the parent that explains how to clone and update submodules This saves time and prevents passive aggressive Slack messages
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.