Auto syncing git based Obsidian vault
By goz
My notes have been in a git repo for almost 10 years and this has worked pretty well for me, especially since I worked almost exclusively from the command line with VIM. However, a few months ago I added Obsidian to the mix, which meant I had to re-think how to sync my notes
git repo.
How it started
I have a VPS where I do most of my writing. This is also where I run automated jobs, such as adding calendar events and completed to dos to my journal. To make this as automatic as possible, I created a a sync repos service.
# sync-repos.service
[Unit]
Description=Automatically does git push and pull on a number of repos
[Service]
Type=simple
ExecStart=/path/to/script/sync-repos.sh
Nice=19
[Install]
WantedBy=sync-repos.target
and a timer to run every two minutes:
# sync-repos.timer
[Unit]
Description=Automatically does git push and pull on a number of repos
[Timer]
OnBootSec=1min
OnUnitActiveSec=2min
[Install]
WantedBy=timers.target
The sync-repos.sh
script is about as easy as it comes:
#!/bin/bash
cd ${HOME}/notes
gstatus=`git status --porcelain`
if [ ${#gstatus} -ne 0 ]
then
git add --all
git commit -am "Git Sync `date` on `hostname` $gstatus"
git pull
git push
else
git pull
fi
This does a great job of keeping the repo in sync. However, now with Obsidian, I need to have the repo synced locally.
Syncing before starting Obsidian
In Obsidian I use the obsidian-git plugin to push and pull during a session. However, I would have strange issues every once in awhile at start up, especially with settings. There may be a setting that I can fix, but I found that syncing before starting and after quit to be the most reliable.
So, for macOS and Windows, I have a .sh
launcher and a .bat
launcher for each OS. (I haven’t looked at doing it on my Linux machine yet.) In it’s simplest form, the scripts do a git pull
before launching Obsidian. Then they wait for Obsidian to quit which is when they add all of the new files, git commit
and git push
.
Windows
The Windows batch file requires git to be installed and set up to clone the repo.
REM Saved as startobsidian.bat
c:
cd "\Path\To\notes"
git pull
start "Obsidian" /MIN /W /D"C:\Users\gozar\AppData\Local\Obsidian" "C:\Users\gozar\AppData\Local\Obsidian\Obsidian.exe"
git pull
git add .
git commit -am "Saving my vault..."
git push
Simple right? Except when you launch it you get a terminal window and you can’t put it in the taskbar. However, that can be fixed. The batch file is saved in my notes
repo so I can easily use it on any machine after I’ve synced the repo.
But, we want to launch it without the window and add to the taskbar. Right click on the batch file and create a shortcut. You can place the shortcut anywhere, but I put it on the desktop.
Right-click on the shortcut and select properties. In Target: you will prepend c:\Windows\system32\cmd.exe /c
so the entire target looks like this:
c:\Windows\system32\cmd.exe /c C:\Path\To\notes\startobsidian.bat
Hit OK and now you can double-click on the shortcut and/or drag it to your taskbar.
macOS
In my notes repo I have a bash script named mac-startobsidian.sh
, made executable, and runs the following:
#!/bin/bash
cd ${HOME}/notes
git pull
open -W -a Obsidian.app
git pull
git add .
git commit -am "Syncing after Obsidian closes"
git push
You have several options to launch the script, I recommend buying Alfred, but if you don’t have Alfred you can create an automator workflow to run the script.
In Alfred I created a workflow called Obisidian Git
. I added a keyword step for obsidian
which tied into a bash script. Since the script is so simple I copied it into the workflow.
However, when launching you’ll see Obsidian.app
and Obsidian (Git)
.
Not only is that ugly, it means I can accidentally launch the wrong one! Luckily, Alfred has you covered. Add the tag alfred:ignore
to the Obsidian application and it won’t show up in the launcher.
Everything in sync
Now I can launch Obsidian and know that any changes will already have been synced. The one caveat to this set up is if I don’t close Obsidian but open it up on another device and make changes to the settings. When I return to the first Obsidian machine and quit, it will overwrite any changes made to the setting on the first machine. It may merge them correctly too, it’s pretty random.
Now go forth and sync with impunity!