CCH

Using Git Submodules

2021-12-09

I was never happy including blog posts with this blog's main repo. It's the easiest way to work but content and architecture should really be separate. I'd heard about git submodules a while ago but had never used them and thought this would be a good place to try.

I have two folders for posts tech and other-posts and it makes sense to have these in their own repo and include these as submodules. It seemed fairly staightforward. To add a repo as a submodule you execute the following:


git submodule add https://github.com/user-name/repo-name local-folder-name

The local-folder-name argument is optional but in my case I wanted a short folder name and not the longer name of the repo. However, I hit some problems similar to this StackOverflow question here. I was already using ssh for my Github connections so I was a bit confused. In the end I deleted all my ssh keys on my Github account and tried from scratch but this didn't help. It turns out you need to make an amendment to the .gitconfig file:

[url "ssh://git@github.com/"]
  insteadOf = https://github.com/
[user]
  email = name@example.com
  name = Your Name

All these entries are important if you're using ssh, including name and email address. It isn't obvious but if you try to use something other than https with the git commands then it won't work (I'm using Debian and it's possible it's a Linux issue from a quick Google search).

If you run the ssh -T git@github.com you will likely see the following message:

successfully authenticated but github does not provide shell access

Again, this threw me somewhat and was the reason why I spent time tinkering with the https addresses (changing them to use ssh) all to no avail. If you have everything configured as above then the message can be ignored.

A couple of other gotchas ...

As anyone knows Debian isn't the most up to date and if you want something other than an empty folder with your submodule then you need to run the following with older Git versions:

git submodule update --init --recursive

Also, if you're having issues adding a submodule and wonder why it might be you've already tried and there's a reference to it that's hard to spot. Check the .git/modules/ folder and remove any entries you see that you're having problems with. I was pulling my hair out with this one trying to add my other-posts folder but once I deleted the entry in the Git folder everything worked.

Useful Links