Loading...
 
JoiWiki » Developer » Applications » Git » Git Concepts and Setup » Submodules in Git Submodules in Git

Submodules in Git

 

Submodules are wonderful things and enable me to work in the way that I like as far as my code base is concerned, there are lots of different approaches and this is tailored around mine, using C# and Visual Studio.

 

My Goal

I like organising shared code into library assemblies that I can reference all over the place, it means that the logic is reusable if you want to share functionality with another application and more than that, if you want to use a library in another solution - it'd be nice to be able to keep track of everything in a logical way. So the idea is to have a single instance or repo for a shared library and be able to update it from multiple solutions and pull the latest changes.

 

How it's Laid Out

A submodule is a git repository inside a git repository, the outer or main repository will track a pointer to the commit of the submodule but otherwise it will ignore everything within the submodules file structure. That means two sets of changes to keep track of at any one time but it also means that you can share changes in the submodule independently of changes to the main repository which for me at least, is very nice!.

 

Things to note with this Approach

The way that I'm suggesting submodules be used here means that you'll have a separate git instance for each submodule in your repo. That means if you've got a solution that you want to use a shared library in a submodule with, you'll open a new bash console for the submodule and track any changes in there, pushing from your submodule branch will track against the shared remote repo as any other project that uses it. The main solution bash window will only track changes made to the file structure outside of the submodule. Remember to push your changes for each submodule that you make changes to if you want them to be available elsewhere!.

 

As as writing I realise that this is an article that I really need to write in its entirety but for right now I'm just going to put a few notes on here, this is a wiki after all:

 

Turn a Project in your Solution into a Submodule

Sometimes you're working on something and think that it's so good that other applications might want access to the logic - here's how you go about turning a project into a submodule (note; there are definitely easier ways to do it than this and I'll update it later but for now, this works!):

  • Create a new repository on your remote and name it the same as the folder you're going to grab.
    • Clone the new repo just anywhere onto your system.
    • Fill the repo with your desired content (by copying and pasting it in from wherever else you're working with it) and push to the remote.
  • Commit your solution so that you've got a backup in case anything goes wrong.
    • Delete the subfolder that you've just copied to a new repo (it's safe don't worry!).
    • Make another commit so that the subfolder you've deleted is removed.
  • Now follow the steps below to add your project back into your repository as a submodule.

 

Add an Existing Repository as a Submodule

Really simple, just go to the bash console for the project you'd like to add the submodule to and enter the following command with the clone link for the repo you want:

#git submodule add git@bitbucket.org:[UserName]/[RepoName].git
git submodule add git@bitbucket.org:JoiB/myrepo.git

 

Clone from a Repository that uses Submodules

If you're cloning a repository that has submodules in it you'll need to take a few extra steps to take to grab the latest versions of everything:

 

 

 

 

 

 

 

Created by JBaker. Last Modification: Friday January 20, 2023 21:38:48 GMT by JBaker.

Developer