Andy's Cafe

Git Submodules

Last updated :

The first time I learned about git submodules was actually from working with hugo.

The documentation used to describe how you can have your main branch live in a directory that points to a diffent origin than your current branch.

It’s a cool feature that I never remember how to use, so here are notes I took after rediscovering it recently.


To add a submodule

git submodule add https://github.com/some-other-repo

To clone a submodule locally

git submodule update

To remove a submodule

git rm path/to/the/submodule
git commit -m 'Removed a submodule'

Related to the use case described by the hugo docs, git worktree can also make another branch live in a directory.

Take the repo I use to manage this site, for example. I want to write on the dev branch, so my repo is checking out dev most of the time.

But hugo builds to a folder called public. And I want all of the contents of public to be the stuff that lives on my master branch. So all I have to do is add a worktree in my directory under the name public. That makes the folder exist as master, so then every time hugo builds and changes something in there, I just add everything and commit to deploy the site.

Reply via email

Tags

#hugo   #git   #golang