The Git submodule concept

The Git submodule concept

I needed to clone a project that relied on multiple other projects and libraries hosted in different remote repositories. To make it functional, I had to combine all these dependencies into a single local repository alongside the main project. To achieve this, I used the Git submodule concept. Git submodules allowed me to include those external projects and libraries within my main project repository, making it easier to manage and track their versions and updates as part of my project development.

1st step ->Initialize the Main Repository

git init <main_repository_directory>        

2nd step -> Clone the Main Repository

git clone <main_repo_url> <main_repository_directory>        

3rd step -> navigate to the directory where the main repository is located

cd <main_repository_directory>        

You should replace <main_repository_directory> with the actual path to your main repository's directory on your local file system. For example, if your main repository is located in a directory called "my_main_project," you would use the cd my_main_project command to navigate to that directory.

4th step -> Add the First Submodule

git submodule add <remote_repository_url_1> <submodule_1_directory>        

Here, This command adds a submodule for the first remote repository to your main repository. Replace <remote_repository_url_1> with the URL of the first remote repository, and <submodule_1_directory> with the directory name where you want to clone the submodule.

5th step -> Initialize and Update the First Submodule:

git submodule init        
git submodule update --remote --recursive        

After adding submodules, you need to initialize them and update their content using above commands. Here, The --remote flag ensures that the submodules are updated to the latest commit of their respective remote repositories. The --recursive flag initializes and updates all nested submodules within submodules.

6th step -> Add the Second Submodule

git submodule add <remote_repository_url_2> <submodule_2_directory>        

7th step -> Initialize and Update the Second Submodule

git submodule init        
git submodule update --remote --recursive        

To update all submodules

git submodule update --remote --recursive        

To update a specific submodule

cd submodule_1_directory
git pull origin master  # or the branch you want to update
cd ..        

To update the main repository

cd <main_repository_directory>   #Navigate to the main repository directory
git add .
git commit -m "Commit message"
git push origin master   #replace origin and master as needed.        

Above commands were quite useful for me when I had to clone a project that depended on various other projects and libraries stored in different git repositories. I think this could also be helpful for you as well.

Thank you.

要查看或添加评论,请登录

Bhathiya Bandara的更多文章

社区洞察

其他会员也浏览了