home

org->git

Table of Contents

1. Intro

Emacs with org mode is a fantastic note taking system that lets you store notes in text files. One drawback of storing notes in text files is that they aren't accessible online. This guide will show you how you can mitgate this risk by using git to backup your notes. Once we're done well be able to push and pull notes from a remote server.

This guide assumes your already familiar with git.

2. Script

Before beginning, make sure that you have a git repo in your notes directory. You can create it with git init if it doesn't exist.

Our first step is to set up a script that can automatically commit changes and push them.

#!/bin/bash
ORG_DIR=/home/$USER/org
printf "Started"
printf "  date=$(date)\n"
printf "  user=$(whoami)\n"

cd $ORG_DIR
/usr/bin/git add .
/usr/bin/git commit -m "AUTO - $(date)"
/usr/bin/git push

printf "Done"

The script is pretty straightforward. First, print a timestamp and who is called the script. This is useful in debugging issues with the script. Next, change directory to the directory with your org notes. Make a commit. I like to use a timestamp for the commit message but this can be changed. Finally, push the changes to the remote server (maybe to your own git server?).

3. Scheduling

Now that we have our script we need to schedule it. I'll be using systemd. Cron also works fine. If your curious on the differences between the two here are some thoughts on using systemd vs cron.

Here is a systemd unit that will run our script:

[Unit]
Description=Syncs the org file to github.

[Service]
Type=simple
User=$YOUR_USERNAME_HERE
ExecStart=/bin/bash $PATH_TO_YOUR_SCRIPT_HERE

And here is a systemd timer that will schedule the unit to run every day at midnight:

[Unit]
Description=Timer for the org-sync service.

[Timer]
OnCalendar=*-*-* *:0:0
Persistent=true
Unit=org-sync.service


[Install]
WantedBy=timers.target
If your hosting your repo on github you'll be required to generate keys for authentication.

You'll want to create keys without a password since you won't be able to input a password when your script is automatically run. You'll also want to add your keys to your ~/.ssh/config file with entry like the one below. This will automatically use your keys when you push code to github.

  Host github.com
      IdentityFile ~$PATH_TO_YOUR_KEYS	

4. Extra

Being able to pull org notes down onto any machine is awesome if you have your emacs config in org! To set up emacs on a new machine all you have to do is install emacs, pull down your repo, run org-babel-tangle on your config and your ready to go!

Date: 7.30.23

Author: Zach Dingels

license