Skip to content

Instantly share code, notes, and snippets.

@Makeshift
Last active March 28, 2024 02:33
Show Gist options
  • Star 97 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save Makeshift/43c7ecb3f1c28a623ea4386552712114 to your computer and use it in GitHub Desktop.
Save Makeshift/43c7ecb3f1c28a623ea4386552712114 to your computer and use it in GitHub Desktop.
Tutorial for automatically syncing an Obsidian vault with Git on an Android device

How to sync Obsidian with Git on Android

Limitations

  • If Termux is closed in the background by Android, the cron service will stop updating your repository and you must open Termux again. Refer to instructions for your device model to disable the killing of certain background applications.
  • This may negatively affect your devices battery life. I'm not entirely sure yet.

Setup

  • Install Termux – Apps on Google Play
  • Open Termux, run termux-change-repo. Press the ↓ button and press spacebar to tick all repositories, then press enter to move to the next screen
  • Press ↓, then spacebar to tick the "Mirrors hosted by Albatross", press enter
  • Run pkg install git -y
  • Run termux-setup-storage
  • Run cd storage/shared (If you get permissions issues, refer to this page)
  • Run git config --global credential.helper store
  • Run git config --global user.email "<your_email>"
  • Run git config --global user.name "<The name you want on your commits>"
  • Run git config --global pull.rebase true
  • Run git clone <your repository> and enter your login when prompted. You may need to create a personal access token if you're using GitHub.
  • Install and open Obsidian
  • Click "Open folder as vault", click on your phone name at the top to navigate to the top directory, and click on your git repository name. Then click "use this folder"
  • With this setup so far, you will need to manually go into the folder in Termux and type git pull. If you'd like to create shortcuts to do this on your homescreen, see this guide

To enable auto-syncing

  • Run pkg install cronie termux-services
  • Restart Termux by typing exit.
  • Run sv-enable crond
  • Run crontab -e and enter */30 * * * * ~/sync_repo.sh (This syncs every 30 minutes)
  • Click the CTRL button, and type x. Type y and enter.
  • Type nano sync_repo.sh and enter:
#!/bin/bash
cd ~/storage/shared/<your repository name>
git add .
git commit -m "Android Sync $(date)"
git pull
git push

(This is a very basic sync and will not handle things like merge conflicts)

  • Click the CTRL button, and type x. Type y and enter.
  • Run chmod +x sync_repo.sh
  • Test your script by running it like so ./sync_repo.sh

Assuming Android doesn't kill the background service, it will now sync Obsidian automatically.

@lead0r
Copy link

lead0r commented Nov 15, 2023

@DovieW thanks, that helps a lot. obviously, my git knowledge wasn't the best here. Concerning the .obsidian subfolder, I was assuming that that was device/client-specific, hence, I wanted to ignore it. But I'm gonna use only those you listed in your repo thanks

@Denis-Alexeev
Copy link

I use obsidian-git plugin https://github.com/denolehov/obsidian-git instead of cron. It works on android as well.

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