Git submodules helpers guide

Git submodules helpers guide

Git has became the most popular subversion system. Not only it is free and open source, but it also provides tons of really nice features available for everyone to use.
A really nice feature that git provides out of the box is the ability to work with submodules.

Why submodules ?

It probably happens to everyone at some point to download a git package from some open source place, for example from github and to use it as a dependency inside a project.
In the past, before submodules existed, the traditional way was to just download the open source files and place them inside your project working directory and track them the same way you track your regular changes. The main issue with that is when the original creator of that library updates it, than you have to go online, download the new version and go back to your project and put the new files and make a new commit. It might not sound that bad, but when you have tons of dependancies like this, it can become quite a daunting task to manage all that. The solution for this madness is submodules.

What is git submodule?

Submodule is simply reference within your git project, that points to another git repository and more specifically to exact commit in that repository. For example if you want to use jQuery as a dependency, all you have to do is add https://github.com/jquery/jquery.git like a submodule in your project, and you will no longer have to download manually jquery and place it in your project.

How to add external source as submodule?

For example if you want to add jquery to your project as submodule and if you have a specific location in mind for it, for example /lib/jquery.
The way to do it would be:

git submodule add https://github.com/jquery/jquery.git lib/jquery
git add .gitmodules
git commit -m “jquery submodule added”

How to revert a Git Submodule pointer to the commit stored in the containing repository?

Sometimes by accident you delete your submodule folder, or may be you cloned your project and if you see your submodule folder empty the way to fix it would be:

git submodule update

How do I download the entire repository and automatically get all the necessary submodules associated with it on a empty folder?

Suppose your join a new team and they are using submodules, you are simply given the url for the repository eg. git://github.com/foo/bar.git. What you should do to properly download everything, including all submodules would be

git clone --recursive git://github.com/foo/bar.git

I cloned a git project into my folder, but all the submodules directories are empty, how do I fix that?

Lets say you cloned your project, but you didn’t get the submodules, to download the required submodules you need just to run:

git submodule update --init --recursive

I tried “git submodule update”, but it doesn’t work.

Lets say you have submodules, but you want to get the correct submodule for the branch that you are working on and lets say when you do “git submodule update”, you still don’t get the correct version. The chances are you have changes within your submodule folder that specific to you and you have 2 options: either commit the changes or discard them. Here is how you would discard them and get the correct changes from the branch submodule, I will use ‘lib’ like example folder for the submodule. But make sure you really want to discard your submodule changes before executing the commands below:

cd lib
git reset --hard
git clean -df
cd ..
git submodule update

How to get the latest version of a submodule?

Lets say you know there is a newer version of a submodule, for example jquery and you want the latest version of it in your project.
This is how you should do it, but keep in mind you might not always want to use master, you need to use the specific branch of the submodule!

cd lib/jquery
git checkut master
git pull
cd ..
git add “lib/jquery”
git commit -m “jquery is updated to the latest verion”
git push

How do I commit changes to the submodule itself?

Now this process requires few steps.
First you need to know on what branch are you going to put your changes, lets suppose for now you want to put all the changes into master.

cd lib/jquery
git checkut master
git pull

And now lets say you want to make some changes, for example add a file

touch addnewfile.txt

At this point, your submodules folder is changed and you have new content, all you have to do is commit the new content and push the changes to the submodule and than update your main repo to point to the track that change.

# here you can add multiple files, or if you want to add all possible changes, just do “git add -A”
git add addnewfile.txt
git commit -m “new file addnewfile.txt added to jquery”
git push
cd ..
# this will point your repo to the newly updated version of jquery
git add “lib/jquery”
git commit -m “jquery is updated to the latest verion”
git push

Leave a Comment

Your email address will not be published. Required fields are marked *