Skip to content

Instantly share code, notes, and snippets.

@tallguyjenks
Last active August 22, 2023 19:55
Show Gist options
  • Star 56 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save tallguyjenks/ca3339b8b5353159f631836268e3f791 to your computer and use it in GitHub Desktop.
Save tallguyjenks/ca3339b8b5353159f631836268e3f791 to your computer and use it in GitHub Desktop.
ZettelKasten Sync Code
# To permanently cache the credentials
git config --global credential.helper store
# To ignore files that could cause issues across different workspaces
touch .gitignore
echo ".obsidian/cache
.trash/
.DS_Store" > .gitignore
# Making out local ZettelKasten into a local Git Repository
git init
git add .
git commit -m "init"
# Pushing our local repository into our remote repository on GitHub
git remote add origin https://github.com/USER/REPONAME.git
git push -u origin master
# Making a new script to automate our repo management
touch zk_sync
chmod +x zk_sync
# -e: edit your crontab file i.e. your list of cronjobs
crontab -e
# My Cron Job:
# */30 * * * * /Users/bryanjenks/.local/bin/zk_sync >/dev/null 2>&1
#!/usr/bin/env sh
# ^^^^^^^^^^^^^^^ This says find the first instance of a sh (shell)
# binary and use that shell to execute these commands.
# There is little to no complexity here and no bashisms so it
# should work just fine on most systems and instances of shells
# (bash, zsh, sh, etc.)
ZK_PATH="PATH TO YOUR VAULT"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ We are assigning the variable `ZK_PATH`
# with the (maybe) long string to our vault's location (mine is super
# long so this makes the final command look cleaner,
# it's unnecessary if you care)
cd "$ZK_PATH"
# ^^^^^^^^^^^ cd: Change Directory to your vault's location
git pull
# ^^^^^^ So if any changes occurred remotely or on another machine
# your local machine knows to pull those changes down instead of
# having to wait for a local change to run the script
CHANGES_EXIST="$(git status --porcelain | wc -l)"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ we are assigning
# a value to the variable `CHANGES_EXIST`, the value is the output
# of `git add --porcelain` which outputs a simple list of just the
# changed files and then the output is piped into the `wc` utility
# which is "word count" but with the `-l` flag it will count lines.
# basically, it says how many total files have been modified.
# if there are no changes the output is 0
if [ "$CHANGES_EXIST" -eq 0 ]; then
exit 0
fi
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The whole if block is saying
# in plain english: if there are no changes (CHANGES_EXIST = 0)
# then exit with no error code `exit 0` if there are changes,
# then continue on with the script
git pull
# ^^^^^^ git pull: this will look at your repo and say "any changes?"
# if there are they will be brought down and applied to your local machine
# In the context of a team environment, a more robust approach is needed
# as this workflow doesnt factor in branches, merge conflicts, etc
# but if you leave your home machine, do work on the work machine,
# push to the remote repo before you return to the home machine, then
# you can just get the latest changes applied to the home machine and
# continue on like normal
git add .
# ^^^^^^^ git add. = add all current changes in the repo no
# matter the level of nested folders/files
git commit -q -m "Last Sync: $(date +"%Y-%m-%d %H:%M:%S")"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# git commit -q -m: this says we are committing changes to
# our repo, -q says BE QUIET no output prints to terminal
# if ran manually, -m defines a message for the commit log
# the -m message is "Last Sync: $(date +"%Y-%m-%d %H:%M:%S")" this
# runs the command date with the formatting arguments for a
# date in YYYY-MM-DD HH-MM-SS format as your commit message
git push -q
# ^^^^^^^^^ git push -q: push the changes to github and
# BE QUIET about it The semicolons between commands are
# just saying run each command and then run the subsequent
# command, they're just separators
@pvroegh
Copy link

pvroegh commented Oct 19, 2022

@tallguyjenks, thanks for this script!
For those who want this in Powershell, I've ported the script: https://gist.github.com/pvroegh/633da28e615fe0e7101dc3a206b2b31a.

@phense
Copy link

phense commented Aug 12, 2023

It is generally a better idea to rely on git than on something like Google Drive (with file-based snapshots) and Obsidian's file recovery plugin.

The major downside of using Git and Obsidian is the following: Git makes delta-based snapshots of your files. Let's say you have a big text file. You save it in git, and it will save the whole file. You change 10 lines and make a new git commit. Now it won't save the entire file again but only the changes. Keeping that in mind, in Obsidian there is some internal refactoring going on. Let's say you are working on the same markdown file, but now you include 10 pictures. You put those .png files in the same directory as the markdown file, and you do a git commit to save your changes.
After working on your file, you notice that your directory has become a mess of files and should be cleaned up. Now you have a decision to make:

  • Make a "media" subfolder and drag and drop your image files in there, within Obsidian. This allows Obsidian to change the links in your markdown file dynamically so that you don't have to do anything yourself. But git cannot do delta's on media files. It only knows that you "deleted" your 10 images from this folder, and you "added" 10 new images in the "media" subfolder. Sure, it will "delete" the old images, but they are still within your repository, together with the same "new" images in the new subfolder. They now exist twice, or how many times you move them around.
  • Instead, you open a console and manually call the git mv command on each individual image file. This will allow git to recognize that the images just moved, and will not add them a second time. But Obsidian will not realize the changed file location, so you have to manually change the image-links in your markdown file.

This is not a big problem initially, especially not the example I described. This was only to describe the issue.
Now imagine you have a main structure of your obsidian vault, and you realize that it would be a great idea to prefix the main folders. Something like this: "000 - Inbox", "100 - Daily", "200 - Projects", ... , "FFE - Meta" (obsidian related files, templates, dataview views, etc.), "FFF - Archive".

Your obsidian vault is 350 MB, most of it from images and maybe PDF documents. You rename your entire folder structure and do a git commit. You will realize that your git repository suddenly doubled in size because it deleted all the files and added all the files under a new folder structure, but kept the references to the old files inside the repository. Git will have also lost all "delta"-changes-references for the new file. Even though it may have 10 different changes to your "thesis.tex" file, for example, that is only true for the old thesis.tex file, not the new thesis.tex file.
Change something in your folder structure again, for example make a "FFF Media" folder and movie all the media files into it, and suddenly, your git repository expanded to almost 1 GB.

I have yet to find a fix around this issue.
The alternative is using obsidian synch, or the "Remotely Save" - plugin. Though, people complained about that particular plugin loosing data / deleting files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment