If you’re tried keeping a git repo in an iCloud (or Dropbox) folder before,
you know it works fine — until it doesn’t. Something inevitably happens
one day while one of your devices is syncing, and your repo is corrupted, such
that a bunch of your files look like they’ve been changed. A workaround I’ve
been playing with is custom
$GIT_DIR set via direnv. Here’s how I set
For a little more background, I’ve been using Obsidian for about 6 months
to manage my notes for work. I had previously been using a simple
~/work/notebook directory full of Markdown files and editing in Vim. I keep
a private git server on a Raspberry Pi that I use to backup my notebook.
When I made the switch to Obsidian, I naturally wanted to access my notes from
my mobile devices. I already knew that git + cloud syncing was a bad idea, but
feeling lazy, I tried syncing my whole notebook directory1 —
all. It worked fine for a month or so, until I would randomly switch from my
desktop to laptop and
git status would report a bunch of changed files.
I knew about
$GIT_DIR, which Git Internals - Environment Variables
GIT_DIRis the location of the
.gitfolder. If this isn’t specified, Git walks up the directory tree until it gets to
/, looking for a
.gitdirectory at every step.
I already use direnv, so setting a shell variable in a specific folder is easy.
First, I moved the
.git/ dir out of the main project folder (notice the
subtle difference with the 2nd
mv ~/work/notebook/.git ~/work/notebook.git
Next, I told direnv to tell Git I’d like it to use that separate directory:
echo 'export GIT_DIR=~/work/notenook.git' >> .envrc
Testing that it works:
cd ~/work/notenook git status ## master...origin/master
This has been working pretty well for me for a few months. I still get the peace of mind of having my content backed up to git, and iCloud hasn’t gotten in the way.
I have run into a couple minor issues:
- I use Vim to work with my notes sometimes too. I use vim-plug to
manage my Vim plugins. If I try
~/work/notebook, it refuses to run because I’ve set
$GIT_DIR. Not really a big deal — I just
cdto bounce home (you did know that
cdwithout args takes you
$HOME, right?) then try again.
git clone --baredoes not work for duplicating this setup on a new machine. I had tried
git --bare clone email@example.com:notebook.git ~/work/notebook.git, but
cd ~/work/notebook; git statusreported
fatal: this operation must be run in a work tree. I had to
git clone firstname.lastname@example.org:notebook.git ~/work/notebook-new && mv ~/work/notebook-new/.git ~/work/notebook.git.
Hopefully this setup continues to work 🤞
I actually keep the real files for my notebook in
~/Library/Mobile Documents/iCloud~md~obsidian/Documents/notebook. But, that’s a pain to
cdinto, so I symlinked
~/work/notebookto it. ↩