Subtrees and Submodules - 14 days of Git
Tags: 14daysofgit, git, devops
Published: 2022-10-06 07:46:05

Today is the last official day of my[ #14daysofGit](https://www.techielass.com/tag/14daysofgit/) learning journey, and today's topic is subtrees and submodules!

### **What is a git submodule?**

Git submodules allow you incorporate and track version history of external code.

Sometimes what you are working on, will depend upon external code.  You could just copy and paste that external code into your main repository and use it.  But there is the obvious disadvantage here of having too manually maintain that code and grab updates when they arise.

You could use something like a package management system to help maintain the code, Ruby Gems or NPM.  But again, there are downsides to this approach as well.

Neither of these methods incorporate methods to track edits and changes of the external repository.

A Git submodule is a recording within a Git repository that points to a specific commit in another external repository.  

Submodules are static and only track specific commits, they don't track git refs or branches. And don't automatically update when the external repository is updated.


### **When should I use a git submodule?**

The use cases for Git submodule are:

* When you have a component, you rely on that isn't updates very often and you want to track it as a dependency.
* When you are delegating a piece to a third party, and you want to integrate their work at a specific release point.
* When you rely on an external component that changes to fast, and you want to lock the code to a specific commit or point in time.


### **What is git subtree?**

Git subtree lets you nest a repository inside another as a sub directory.  

> It's worth mentioning that there is also a merging strategy called[ subtree](https://docs.github.com/get-started/using-git/about-git-subtree-merges).  So be careful with this term and ensure when talking or referencing subtree.


### **Git submodule vs git subtree**

There are a lot of articles that say submodules aren't something you should use, and subtrees are the way forward.   There are some benefits of subtree over submodule.  But subtree has its own disadvantages.

If we look at why people prefer subtree over submodule:



* It's an easier workflow.
* There is nothing to learn when using subtree, they can ignore the fact they are using subtree to manage dependencies.
* Subtree doesn't add metadata files like submodule does.

The drawbacks of subtree though are:

* Contributing back to the original code is more complicated.
* You need to be careful not to mix commits with your project and the third-party code.

Both have their use cases but from the research I have done the preference seems to be _git subtree_, as it overcomes the drawbacks of git submodule.


### **14 days of Git**

Today has been an interesting day of learning for my last day.  It's been mostly theory based.

This[ #14daysofGit](https://www.techielass.com/tag/14daysofgit/) learning journey has certainly been a journey for me.  I've learnt a lot. I've cemented some of the basic skills and knowledge that I had around[ Git](https://git-scm.com/), while also adding more of the complex concepts to my skillset.

If you wish to copy my journey my learning plan can be found at[ https://github.com/weeyin83/14daysofgit](https://github.com/weeyin83/14daysofgit) Feel free to contribute there with suggestions of other things to learn etc. Happy to hear suggestions on how to improve my learning journey.

And if you are new to my blog then please do[ subscribe](https://www.techielass.com/newsletter) and get notified of new posts that I do!