Git submodules have always been mysterious to me, but since I've done some challenges on DevChallenges.io I figured creating a repository and linking to my challenges from that one would be a great way to learn about them.
Devchallenges is designed for everyone who wants to be a software developer, it consists of different projects or you might call it challenges. Each challenge is different and will test you different aspects of coding. It includes small user stories and beautiful designs. It reflects real-life tasks that a developer do daily.
You can also find different paths that will help you navigate your career path. Currently, it has only responsive and front-end path, but I am working on more challenges. You will get a certificate when you complete a path.
- Snippet from Thu Nghiem's post devchallenges.io - 16+ FREE Projects with beautiful designs
Learning something new can be challenging at times, so finding a way to make it fun has always seemed to help me.
This time around I wanted to learn about submodules, so linking my challenges to a central repository was a great way to learn, as a bonus, it also gives me a central repository to pass around instead of passing a link to separate challenges.
The official documentation of submodules describes the issue of wanting to use one project inside another project.
Git addresses this issue using submodules. Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.
- Git Tools - Submodules
To use a submodule you add them as you would a remote repository, but using the
submodule command instead of
$ git submodule add https://github.com/thinkverse/404-challenge
This will create a
404-challenge directory in your repository, alternatively, you can pass in a string overwriting the default directory name.
$ git submodule add https://github.com/thinkverse/404-challenge 404
You will also notice that git has created a new file called
.gitmodules containing configuration about your newly added submodule.
[submodule "404"] path = 404 url = https://github.com/thinkverse/404-challenge
So you've added a submodule to your repository and you submodule has revived an update that you would like to use, don't fret, just like a regular repository you can fetch updates from a submodule as well.
$ git fetch
Then you can merge the updated changes.
$ git merge origin/master
That might get pretty old, so git has an easier way to fetch an update using the
submodule update command.
$ git submodule update --remote
Not going to lie, I haven't gotten that far in my learning yet, so I'll ask that you refer to the documentation. 👍
Working with submodules isn't all rainbows and sunshine though, reading around in the documentation it seems there are issues pertaining to branches in older versions of git.
For instance, switching branches with submodules in them can also be tricky with Git versions older than Git 2.13. If you create a new branch, add a submodule there, and then switch back to a branch without that submodule, you still have the submodule directory as an untracked directory
So do keep that in the back of your mind when working with submodules.
For now, I'm going to keep doing my challenges and reading up more on submodules. 🎉
Do you use submodules? Let me know in the comments. 🤘
You can visit my profile on DevChallenges to see my completed challenges, and if you're a member, I'm open to feedback. 👍