[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

4 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.

2 Likes

Hi and thank you for sharing this,

Do we need to root our devices to get this to work? I can’t get the chmod commands to work so none of the scripts have executable permissions here.

Thanks,
Rich

I have sync working from termux terminal, but I haven’t yet figured out how to do the shortcuts - one reason may be the permissions issue above.

For the shortcuts section, does this require installing the Termux:Widget plug-in? I don’t see any documation for how to make these /data/data/com.termux/files/home/.shortcuts folders and friends, and I don’t see any widgets available on my phone to run a script until I downloaded that Widget plug-in (via F-Driod).

Thanks for the help,
Rich

Hi, thanks for creating this guide! Very helpful.

I noticed one mistake: the sync_script should end with git push instead of git pull

1 Like

I’m using a similar approach to automatically sync my vault between my devices. In comparison to the approach taken from @RedeyeFR I use Tasker instead of a cronjob and I’m using the great git-sync script by Simon Thum.
I have documented my setup in the following git repo:

2 Likes

For anybody still looking for this, or wanna improve this method, try this. Ive done some research about the best way to do the remote vault with GitHub.

First of all, On PC, use Obsidian Git community plugin in the Obsidian. It have auto push and auto pull feature. Nice. So now I can write notes in obsidian, it will auto sync to GitHub.

Now how about if I wanna access it in mobile phone? After 1 day searching, the best way is by using Termux, no other apps as simple as Termux. The same as the main answer in this post, but simpler. At first, I thought “I need to to git clone, then create some automation script to git push and git pull whenever I want bla bla bla”. Turn out its much simpler than that.

I use Termux. Setup the storage path. Install git, then git clone my repo, with username email and auth token and stuff (you dont need to install gh, setup ssh, update upgrade, cronjob or anything, the only command you will use ONCE, is only git clone). And then open obsidian on mobile phone, find the vault folder u cloned into your phone. And then, the magic happens, the Obsidian Git plugin from PC is there too!! Its downloaded along with the repo. At first when thr Obsidian Mobile App is emtpy, u cant setup any community plugin. But after u have at least 1 vault set up, u can access the community plugin.

And now, you dont ever need to touch Termux anymore! Because the Obsidian Git plugin on the Mobile App will automate the push pull for you, the same as PC.

2 Likes

Thanks for the guide! I’m at the stage where I need to set up Tasker automation, but that part is not explained beyond the use of Termux:Tasker. Could you please share the Tasker action you use? Or somehow explain what I’m supposed to do?

1 Like

Hey there, here’s how I did it using tasker, hope it finds itself useful.

If you still need help, feel free to tell us !

1 Like

Thanks! Between your guide, and some tinkering, I managed to get the automation working using the sync script and Tasker.

3 Likes

Glad to hear that, have a good day !

You too!

@Feralflora Great that you have managed it to get the sync working!

I have now slightly updated the information on my setup, including a brief introduction to my Tasker:Termux configuration.

@RedeyeFR Great tutorial! It gave me some ideas on how to make my setup a little bit simpler in the future.

2 Likes