Skip to content

Instantly share code, notes, and snippets.

@Makeshift
Last active December 13, 2024 19:11
Show Gist options
  • 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.

@DovieW
Copy link

DovieW commented Nov 15, 2023

@lead0r You can use the following command to basically disable that check for safe.directory with the following: git config --global safe.directory '*'

You can check your global git config file directly to see which folders are considered safe and see if it's being removed: cat ~/.gitconfig

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

@irfandyj
Copy link

Anyone have alternatives to Termux? Seems like my Android isn't compatible with the current Termux.

@lead0r
Copy link

lead0r commented Jun 11, 2024

Anyone have alternatives to Termux? Seems like my Android isn't compatible with the current Termux.

Are you sure it is not related to this: termux/termux-app#4000 ?
If you are on v0.118.0 (maybe from F-Droid or another source), I'd suggest you stay with this version. Quote from the link above: "Basically, PlayStore termux-app v0.120 release is functionally equivalent to v0.108"

@Petersen-Pedro
Copy link

It tells me the following message:
termux-change-repo: command not found

@JOduMonT
Copy link

termux-change-repo: command not found

even if I believe this command is not required anymore since termux use mirror group; it still work

Screen Shot 2024-07-19 at 16 31 37

@JOduMonT
Copy link

JOduMonT commented Jul 19, 2024

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

interesting
would you like to explain me how you managed that ?
personnaly it can't find the command ssh

[EDITED]

I got it; it work if in .git/config of your obsidian repo you use the HTTPS protocol instead or SSH

@studioether
Copy link

Well, when I try to auto sync it asks me to enter my GitHub login details(username and password) and then later on tells me that Support for password authentication was removed on August 13, 2021.

@studioether
Copy link

Well, when I try to auto sync it asks me to enter my GitHub login details(username and password) and then later on tells me that Support for password authentication was removed on August 13, 2021.

@DovieW
Copy link

DovieW commented Oct 27, 2024

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