Russian Doll Git Repositories

I’m in the middle of upgrading Write.app’s editor. Right now it’s a just a very pretty <textarea> with a couple tricks like theme switching, full screen mode, and true tab indents. Lame. So I’m rewriting it using CodeMirror as the base for building off of. Thing is, I’m using CodeMirror as a module within a larger project. I haven’t just cloned CodeMirror and started building on top of it like you’d assume. Instead the repository is set up in such a way that it’s a larger repository with one or more totally separate Git repositories inside of it. It’s a Russian Doll Git Repo. So today I got to work and decided I’d pull down the repo and do a little work on it in my spare time. I cloned the repository just like normal and opened up the project. I looked inside the codemirror directory and it was empty! I’m not new to Git but made a total newbie mistake. You can’t just throw different repos into a larger repository in Git. So how do you manage a project that uses one or more different projects within it while still keeping the inner repositories intact? There are a couple of ways as I found out today.

Git Submodules

Stay away from these! They’re super complex and unnecessary unless you’re super advanced or have some good reason to use one. If you’re reading this you probably don’t need one. Instead, use a different method…

Fake Submodules!

You can fake a submodule! Let me set the stage here first. In my case, I cloned CodeMirror into my repository, which itself is a clone of Fraction.less Boilerplate with a new origin. The main repository makes use of CodeMirror’s code but the codemirror folder is itself a git repo. If you just do a git add . or a git add codemirror then git would recognize that you’re trying to add another repository and would automatically create a submodule. You don’t want that. If that happens and you try to push your main repo, the submodule’s folder will be empty. Because it’s a submodule the expectation is that whoever clones your main repo will need to go inside of the submodule folder and pull in the separate repo’s code. Then all hell breaks loose.

The way around this is simple. Before you add anything to your repo, do this:

Adding a fake submodule
1
git add my-other-repo/

Note the / at the end of that line! Without the trailing slash you’d be adding a submodule (and like I keep saying, bad stuff will happen) but with nothing more than a single trailing slash you’ve got yourself a fake submodule. Everything in the folder will be added to the repository and all will be well with the world. As a bonus, if you ever need to pull anything from the nested project’s upstream it’s as easy as doing a cd sub-project and then git pull origin master.

You should check out Wreditor

The reason I needed to learn this was because I ran into it with my new Write.app related project, Wreditor. Wreditor is an open source browser based text editor built on top of CodeMirror. The idea is to give people a powerful and extremely beautiful browser based text editor with the ability to hook it up to most backends with very little code. It’s like having a ready made, already implemented text editor and all you need to do is hook it up to a database or to S3 or to Dropbox, or local storage, or whatever! Get the source on GitHub.

Web development

« Fraction.less 2.0 Beta The Consequences of Being Too Public »

Comments