Git submodules - Why and How to use them

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to my youtube channel so today we'll be talking about git sub models why you should use them and how to use them if this is your first time on my channel here firstly subscribe and secondly you're welcome on my space here i share things around tech i talk about career and i talk about life and in the first video on my channel i gave an introduction as to what to expect here and a little about myself so if you're interested you can check out that video and then down to what we have for today git sub modules and i've outlined what we'll be covering the first and least is what is a git sub module then use cases of git submodules how to create a github module using git sub modules in your project cloning a project with git submodules things to watch out for when using github modules and conclusion firstly what is a gitsub module a github module is a feature of git that allows you to manage a git project inside of another git project and here's what i mean let's say you have repository a and you have repository b and for some reasons you want to use the the code or the files in repository b inside of repository a now the first process you think of is to copy and paste so you can go to the project for repository b copy and paste search files into repository a locally on your machine now while that can also work that can be strenuous because sometimes you may forget say you make changes to repository b you may forget to copy and paste and even the process of copying and pasting doesn't seem like a nice idea with git sub models instead of copying and pasting you can have let's say you have project a for repository a and you have repository b you can have a subdirectory in project a you can call that uh project b project b is a subdirectory inside of project a so project b can serve as a submodule which is pointed to the repository b and as a submodule it's like a jit project on its own ajit initialize project on its own so project b can push changes to project a to repository b project b can manage its own commits with repository b rep project b can also pull changes from repository b so instead of copying and pasting from representative b to representative a all you are doing is you are pulling from repository b and then those files repository b would be uh would be downloaded in the project b directory so in your project a you can use any of those files any of those shared logic anything you want so this way now you have your project a doing what it's supposed to do and you have your project big directory pulling the changes accordingly that's what sub modes are so like i said they allow you to manage git inside of git now let's look at the use cases of git submodules so the first case which you may have thought of is say you're working on this was actually the uh the use case i had before i discovered kits of modules so i was working on three projects at once and i discovered that these three projects they had shared logic they had shared functions they had shared apis so i found myself always making changes in project a and then i'll copy that change go to project b i'll copy that change go to project c like i had a couple of similar files and it just dawned on me that i should look for a simpler way to manage all of these like just having one location where i can stall of this the first idea i had was npm but you know the caveat with using npm in as much as it can work is that for every change i make to those shared files i would have to update the package version because npm of course doesn't allow the same version for updated packages so i'll have to update the version number then i'll have to publish to npm and then i'll have to pull change batch number push pull that are uh that doesn't look so nice to me so i had to look for something else and then i found beats so beats in my own experience in as much as it could do what i want because bits is like a library that allows you to have collection of files collection of it's just like gates it it manages versions too but this way it's just like a store where you can manage your shared items my challenge with beats was the free tier only allowed me to have one collection so what if i had different collections like an api collection utility collection component collection i would have to pay and i wasn't ready for that so while making my research i found git sub modules so this way i had these three projects and i had a folder which i called shared so this shared folder was a git sub module that was pointing to the shared repository so in that repository i had my shared apis my shared functions everything i wanted shared so project a project b and project c they had this shared folder which is a jigsaw module and whenever i make changes like in project a i could make a change to that shared folder and once i push that change to the shared repository project b and project c can also pull those changes so the very use very first use case is say you have shared logic and you want to share it across various projects git submodules would be a very nice idea another use case is let's well the use cases can be numerous but it all revolves around trying to extract code from an external repository so instead of copying and pasting then you go for git sub modules now that we know what git submodules are and why we should use them we'll be looking at a small git project to learn how to create git submodules so basically we'll be having a sub module that points to another repository can push and pull changes from that repository let's check it out okay the first thing we have to do is create our project folder and i'll call this sub module tutorial then you change your directory to the sub module tutorial now this model tutorial project is going to hold its own files its own content and then it's going to hold uh git sub modules so to make this to make this project for that valid git project then we have to initialize git and once you can try viewing this on vs code or any id of your choice and this is what we have we have the git directory which is the default directory that appears on initialize git but this doesn't have any file of its own so let's create one readme and inside of our file we can have how to create pizza model just some content back in our terminal git status and we have a new file and we could just commit that add readme so now we have this small project sub model tutorial and it has its own file which is a readme now let's create a sub module and for you to create a sub module you must have an existing valid git repository and when you have that you can use this command git sub module add the remote url and the sub module name so this remote url is going to be the git url from the repository that you want to have as a sub module i'll be using gatsby remark liquid tags one of my packages so to get the remote url just the same way you would clone a repository you get it from here copy back in your terminal can change the remote url paste which is the remote url and then the sub module name this is an optional argument if you do not add a sub module name the sub the sub module's name is going to be defaulted by gatsby remark liquid tax but since that's a bit long for me i can change that to shared or i can just change that to tags by creating the submodule it clones into the repository and then it creates a folder called tags in our id this is a folder the tags directory but when you go back to get git status it doesn't see tags as a directory instead it sees it as a sub module so in our id and in our project we can interact with tags as a directory which is holding the content it's pulled from the gateway mac liquid tags but for the parent directory which is submodule tutorial it sees tags as a git module so on cloning that on creating that sub model we have the dot git modules file and this holds all the sub mode that i used in our project and so far we have added only one so the sub module name is tags the part is tags which is just at the root here and then this is the remote url and then we have the tags directory holding everything that it pulled from the gateway mac liquid tags so over here we have the github src test editor config and over here we have dot github src test editor config so we can see here tags is a sub module and as tax is a submodule that means it's a a git project on its own which means we can go into sheet we can go into tags we can run git status we can confirm the remote url which is pointing to liquid tags we can push of course we haven't made any commit yet so everything is up to date and we can also pull since the repository has not been updated yet everything is up to date so tags is a sub module a git project and you can perform every of git operations inside of it you can check the com uh the last commit update package version as you'd see here update package version now the for that test let's create since this project is live already and i want to push something then i can just create a branch to test testing module remember we're still in the tags directory which is a sub module we have created this and let's add a new file called testing.md and in it we just have to test the text testing when you open tags you would find a new file which is testing.md and when you run git status we have our new file testing.md this is on tracked because git doesn't know that this file exists yet now the beautiful thing here is we are sitting in the tags directory when we head back to the parent directory and we do get status you can see that here git knows that there is an untracked content inside of the tags submodule it doesn't have full information about what is tracked what is on track what has been committed what has been staged the parent directory doesn't know of that but the sub module as we saw above it knew that it was on track files they were on track files and this was one of the on tracked files so as would see the parent directory doesn't have full information about the github module it only knows where something is added a new comment has been made it's left for the git submodules to commit those new changes to push or to pull and when we go back to tags we can add the new change you can commit new file and when we go back to the pairing directory and run git status now you can see that the parent directory is informed or is aware that there are new commits in the tags submodule it doesn't know what has been committed yet but all it knows is that there are new comments there and now we can add this and add and update sub module furthermore we can go back to the tags directory since we have a new commit which is the new file we can push this to the testing module branch in remote and right here we can see that we have indeed pushed to this repository from another gate project and once we match this on master any other project that has this gatsby remark liquid tags as its submodule when it runs git pull it will pull the new changes so i believe now you can see how we are sharing logic using one repository as the source of truth and then sharing that among every other project and like i said you can have multiple sub modules so you can go back get sub module add isjet so this is under package of mine and we can copy the remote url come back here paste it i don't need to change the submodule name i'm okay with this back in our id you can see that the git submodules now has two submodules one is tax and the other is is get and then is gate also cloned the repository contents and store them in the escape directory similar the tags we can go into is the ace gate make changes push and pull and back to the main project get status there are there's a new model and we can add that commit add new sub module as we have said in that tutorial we have seen how to create git sub modules and also how to use git submodules so we could go into the sub module directory we could manage gate inside of it we could pull we could make commit we could create our own branches we could push to the repository and just like i said if there was any other project that was using the gateway mac liquid tags as a sub module once the project runs git pull inside of the sub module it will pull these new changes that we have pushed including the branches so we can manage branches across every of this project and then we can have this rematch liquid tax as the main source of truth which means every of our shared logic can be in this remark liquid tags and every of this project will benefit from it the next on our outline is how to clone project with sub module back in our terminal i'm going to head to gtop to create a new repository and i'll call that sub module [Music] tutorial no description it's public none of these create repository and i can copy the remote url and i can add the url here so i can push all my commits to master and then on refreshing the submodule tutorial repository would notice some interesting things so the git modules is a file the readme is a file but then these are sub modules they are not files neither it's a submodule file they are not directories now when we click on the tags it takes us to the last commit in the gatsby remark liquid tags which is the commit that had this new file and similarly when we click on is git it takes us to the last commit that was made before we added the submodule to the git project and the last commit here was the modified configuration something i did last year so that's pretty much it this is the this is the project now these are the sub modules and then these are the files of its own now let's say we're on a different device and we wanted to clone this project something interesting will happen and let's look at how that was so let's copy the remote url back in our tummy now i'll go one step backward just clone and i'm going to call this true shop module tutorial it's cloning then we change directory to our sub module tutorial and we can view that on vs code now here's something interesting so we have our git modules file which has the two git modules meta information about the toolkit modules we have our read me but when we go to tags we discover that tags is empty once we go to ez key to discover that is git is also empty but when you go to git you notice that in the config file here the config file has no information about the sub mode unable to pull the uh the git submodules correctly so now there are two ways to go about this the first way to go about this is to add an extra option when cloning so let's step backward a bit i'm going to clear all of this let's step backward a bit uh on my left here i have the two submodel tutorial um right here i would go to my g tab and i'll try to clone the repository again copy the love pasting it and stopping here i'm going to add the recursive option so what this recursive option does it it oh that was a double hyphen what am i missing out on okay i have to give it a different name so let's call this three sub module tutorial it's cloning just as we can see here sub module is registered for this sub module tags registered for this cloning into each jet cloning into tags so the recursive um helped us to also clone uh just like jit pool it helped us to clone the sub modules accordingly so when we open that so here's what we have so we have the tags the tags now is populated as we expected the is git directory is populated as we expected and when we go to the gate and we check this config file would notice that the sub modules were also added here as we expected and this is the first way of doing it let's look at the second way so in this directory which does not have the sub modules populated there are two things we can do the first thing we can do is get sub module emit and what this does is it goes into our git dot git modules file which is this and then it's going to take this the way it is here and it's coming to the config here to paste it like this that's the only thing the command is doing so we can test that out i'm removing it git submodulated in it enter and then when we come back to the config that was the only thing that the command did but the command has not populated our his git fold and the tags folder yet so the next thing we need to do is get sub module update so what this command does is it goes into our config file the sub modules that have been registered there is going to clone the sub modules accordingly and now when we go back to our id we have the tags populated as we want and we have the is gates populated as we want now there is uh you find this online a lot the git the git submodule uh init command looks irrelevant since it's only copying from the github modules and pasting in the config but that's not all the git submodule in it does and this is why the the command is still relevant let's create another uh project directory i'm going to clone this one more time git clone this for sub module tutorial i'm not adding the recursive and when i open it okay i wasn't supposed to open that so i'm supposed to go into this and open now when i open it as expected the tags is empty the is git is empty and the git config doesn't have the sub modules yet so this is why the git sub module unit is relevant i can come here submodule in it and i can just initialize only the one for tags i don't care about the one for each date once you do that in your config you only have one for that so say you have many sub modules and you're about to be for a particular project you're working on instead of recursively closing everything you can use github model in it to pick just the one that you want which is the tags and now when i run git sub module updates it's only going to clone into tags but it's not going to clone into the isjet and there you have it each jit is still not populated and tags is populated accordingly so there you have it those are things to watch out for when you're cloning because when you clone g kit doesn't do that for you you have to either use the recursive option or you initialize and update every sub-module or you initialize and update only the specific ones that you care about so so far in this video i've looked at how to create sub-modules how to use sub-modules and also how to clone projects with sub-modules and then the last thing on our list is things to watch out for when using submodules so in my experience when i discovered how the use of modules i was very happy i started using it and i was able to extract all of my shared logic within these three projects having them in my shared repository that worked fine but one thing that caught me unaware was i was working on private repositories because it was a private work not something public and i was hosting them or in the on heroku now hiroko and vassal do not support private sub models and that was that's the only cavita found so far they don't talk about private submodules i either have to make the sub modules public or i don't use them they don't support that for now hopefully in the nearest future i think vassal has some plans of supporting that soon but until then they do not support private submodules so if there isn't sub modules make sure they are public else you won't be able to host it on these platforms maybe other platforms support them i'm not sure i'm pretty sure if you're using your own platform maybe like a digital ocean droplet you should be able to use submodules since you are the one performing the deployment on your own but vassal and heroku does not do that and that's the only caveats that's the only thing to watch out for so if you're using git submodules make sure there are public projects and that's it for git sub modules that's all i have for github modules there's actually a lot and i get some words there a lot of options aside apart from in it apart from updates apart from there a lot of other options that can suit every of your other needs but i believe that this video has given you a clear guide on what github modules are how to use github modules how to clone project with git submodules and then one reason why you should subscribe to my channel so thank you for watching i hope you have learned a lot if you really have please share and subscribe if you haven't and see you in my next video
Info
Channel: Dillion Megida
Views: 698
Rating: undefined out of 5
Keywords:
Id: De8Bc1VxcGQ
Channel Id: undefined
Length: 28min 9sec (1689 seconds)
Published: Sun Apr 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.