Statement: The purpose of this post is how to use GIT submudule in the existing repository by using CRUD operation for the same. While working on the different git directories, we don’t want to repeat/copy the source code in these repositories. To avoid the same, we just create the parent repository in which we put the common code and make the child repositories as a git submodule to distinguish the code based on the different features.
Prerequisites: Please ensure to have git installation on your working machine.
- Create a parent repository in your git account (you can clone the existing repository using your username)
- To make use of other repositories as a child in the above parent repository, make use of git submodule using the below command –
tangupta$ git submodule add https://github.com/guptakumartanuj/child-dir.git
- Likewise, you can add the different repositories as a child in the parent repository as mentioned above. Now, you can see these changes reflected in the parent directory.
tangupta$ git status # On branch master # Changes to be committed: # (use "git reset HEAD ..." to unstage) # # new file: .gitmodules # new file: doctrine #
- Commit all these changes and push all the child git directories in the parent.
tangupta$ git commit -m 'Added all the submodules'
tangupta$ git push
- Out of the curiosity, you will find the empty folders for all the submodules added in the parent repository. To sync all the submodules, you need the run the below commands –
tangupta$ git submodule init
tangupta$ git submodul eupdate (You need to do this in case of updation in the child repo.)
- Finally, if you want to clone the parent repo with all the files of child repo then use the below command to do the same –
tangupta$ git clone --recursive (repository)
- In case of removing the submodules, you need to remove the entries from the below mentioned files –
- .gitmodules file (Remove all the three lines added for the respective submodule).
- .git/config file (Remove all the two lines added for the respective submodule).
- Remove the path created for the submodule using the below command –
tangupta$ git rm --cached child-dir
- Finally if you want to pull all the changes which are done in the submodules, use the below command to get the same –
tangupta$ git submodule foreach git pull origin master
Hope, you will get the full understating of how to integrate the parent and child repositories in case cross team working on the same code. GIT rocks !!! Enjoy 🙂