Using DevChallenges.io to learn about Git submodules
Tags: git, github, webdev
Published: 2020-08-07 15:51:57

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.

## What is DevChallenges? 🤔

> 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]

## How do you make learning fun? 🤘

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.

## What is Git submodules? 🙂

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 `remote`.

```bash
$ 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.

```bash
$ 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.

```markdown
[submodule "404"]
	path = 404
	url = https://github.com/thinkverse/404-challenge
```

### Updating a submodule 🎉

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.

```bash
$ git fetch
```

Then you can merge the updated changes.

```bash
$ git merge origin/master
```

That might get pretty old, so git has an easier way to fetch an update using the `submodule update` command.

```bash
$ git submodule update --remote
```

### What about deleting a submodule? 😈

Not going to lie, I haven't gotten that far in my learning yet, so I'll ask that you refer to the documentation. 👍

### Issues with submodules ⚠️

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. 🤘

### Find me on DevChallenges 🔥

You can visit my [profile] on DevChallenges to see my completed challenges, and if you're a member, I'm open to feedback. 👍

[devchallenges.io]: https://devchallenges.io/
[thu nghiem]: https://dev.to/nghiemthu
[devchallenges.io - 16+ free projects with beautiful designs]: https://dev.to/nghiemthu/what-is-devchallenges-io-157a
[submodules]: https://git-scm.com/book/en/v2/Git-Tools-Submodules
[profile]: https://devchallenges.io/profile/Sciv9zfRD8bScT40Ftot