[Guide] Using Git to sync your Obsidian vault on Android devices

Install Termux and Termux Widget

From Github Termux ,[^1] Github Termux Widget [^2] download the APKs and install them. You can use F-Droid as well.

Give Termux access to your storage using the following command :[^3]

termux-setup-storage

Setup Git and Github for Manipulating Repositories

Use the following commands to setup git and github via HTTPS :

pkg update && pkg upgrade
pkg install git
pkg install gh

You can use SSH as well

Login to your Github account :

gh auth login

Update the git configs :

git config --global user.name "name"
git config --global user.email "email"

Now you should clone your repository, try to use the following commands :

git status
git pull
git commit
git push

If you are on Android 12, you’ll get an error with the command required to add the repository to a security list or something like that. Do it, and these commands should work.

Setup the Sync Script Shortcut

Create a directory for the shortcuts :[^4]

mkdir -p /data/data/com.termux/files/home/.shortcuts
chmod 700 -R /data/data/com.termux/files/home/.shortcuts
mkdir -p /data/data/com.termux/files/home/.shortcuts/tasks
chmod 700 -R /data/data/com.termux/files/home/.shortcuts/tasks

Create the sync script :

nano /data/data/com.termux/files/home/.shortcuts/tasks/sync_script.sh

Add the following script :[^5]

#!/bin/bash
cd storage/shared/LifeWiki
git pull && git add -A && git commit -a -m "android vault backup: `date +'%Y-%m-%d %H-%M-%S'`" && git pull

Create the widget and add it to your home screen. That’s it, you just need to launch it to sync your Obsidian vault. It means you need to launch it before and after editing notes. If you putted your script inside ~/.shortcuts it will launch in the foreground, and if it was inside ~/.shortcuts/tasks then it will launch in the background. I recommend using as a widget the one that execute the code in the foreground and keep the other one for the Cron job.

Setting up an Automatic Execution of the Script

If you want to automatically sync your vault every hour, for instance, you can do it using a Cron job. [^6] [^7] [^8]

First, you need to install Cron :

pkg install cronie termux-services

Then, you’ll restart Termux then run the following :

sv-enable crond
crontab -e 

Finally, you’ll end up with the crontab -e command in the nano text editor. Add the following :

* */1 * * * bash ~/.shortcuts/tasks/sync_script.sh

You can find information about Cron job easily on the internet.[^9]

![[cron-job-format-1.webp]]

[^1]: How to install Git on Android with Termux (Step-by-Step Guide)

[^2]: Mobile Sync for Obsidian | Some Thoughts and How to sync your obsidian vault on mobile using git : ObsidianMD

[^3]: Git Guides - git add · GitHub and bash - How to set current date as git commit message - Stack Overflow

[^4]: Obsidian Syncing Alternative | Obsidian-Sync-Alternative

[^5]: Obsidian Github Integration for Sync and Version Control - Share & showcase - Obsidian Forum

[^6]: Obsidian Syncing Alternative | Obsidian-Sync-Alternative

[^7]: Obsidian Github Integration for Sync and Version Control - Share & showcase - Obsidian Forum

[^8]: Guide: Obsidian vault github sync cron on termux : ObsidianMD

[^9]: A Beginners Guide To Cron Jobs - OSTechNix

2 Likes

Edit : The correct cronjob for an edit every hour at xx:00 would be :

0 * * * * bash ~/.shortcuts/tasks/sync_script.sh

I can’t edit the post because it has too much links and I’m a new user.