Skip to content

Instantly share code, notes, and snippets.

@Makeshift
Last active May 6, 2024 17:17
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.

@salluzziluca
Copy link

Total serendipity, thanks A LOT!

@salluzziluca
Copy link

Hello, it keeps saying
remote: Repository not found. fatal: repository 'my repo' not found
and I know that the repo do exist. Could it be a credential problem?

@Makeshift
Copy link
Author

Hello, it keeps saying remote: Repository not found. fatal: repository 'my repo' not found and I know that the repo do exist. Could it be a credential problem?

It sounds like you're not putting in the full repository with the .git extension. It'll be something along the lines of https://github.com/Makeshift/obsidian-notes.git

@merqrial
Copy link

merqrial commented Dec 2, 2022

This is brilliant. Thank you. I completed it just a while ago. With only problem currently remaining is that termux widget does not seem to work for me yet. If symlinks are added in the shortcuts folder they are not picked up by the widget and if I copy the actual files there, it does not work either.

However, both these items are out of the scope for your tutorial and are under the extended part which you linked. You have done a great service by writing this. Thank you.

@Gourav1910
Copy link

I am not able to type password nor token. It just not typing anything.

@Makeshift
Copy link
Author

I am not able to type password nor token. It just not typing anything.

When you type passwords into a Linux terminal, they're usually invisible and give no clear indication that it's accepting input, but it is. Did you try typing your password as normal and hitting enter to see if it is accepting your input?

@lucideuclid
Copy link

Everything went well. Except for when I tried to test the script by running

./sync_repo.sh

I got

bash: ./sync_repo.sh permission denied

@hatosuke
Copy link

@lucideuclid
I have the same error, but it's solved.

When I create "sync_repo.sh" under /data/data/com.termux/files/home/storage/shared/, I can't change permission and I can't run it. So I create it under /data/data/com.termux/files/home/, that's /data/data/com.termux/files/home/sync_repo.sh, I can change the permission, and run correctly.

I hope this will help.

@greew
Copy link

greew commented Apr 15, 2023

An idea (that I used) to be informed about merge conflicts:

cd ~/storage/shared/<your repository name>
git add .
git commit -m "Android Sync $(date)"
git pull
if [ $? -ne 0 ]; then
    termux-notification -c "Obsidian git merge conflict"
    exit 1
git push

Just need to install Termux:API and let it do it's business! :)

@rafaelquintanilha
Copy link

cd ~/storage/shared/
git add .
git commit -m "Android Sync $(date)"
git pull
if [ $? -ne 0 ]; then
termux-notification -c "Obsidian git merge conflict"
exit 1
git push

Thanks! You are missing the fi clause though.

...
if [ $? -ne 0 ]; then
    termux-notification -c "Obsidian git merge conflict"
    exit 1
fi

@greew
Copy link

greew commented May 9, 2023

...
if [ $? -ne 0 ]; then
    termux-notification -c "Obsidian git merge conflict"
    exit 1
fi

Sorry... correct! :)

@DovieW
Copy link

DovieW commented Jun 12, 2023

Termux on Google Play is deprecated. Recommend it from F-Droid rather.

@iomari
Copy link

iomari commented Jun 16, 2023

Termux on Google Play is deprecated. Recommend it from F-Droid rather.

No it's not. I'm looking at it on playstore right now. It was was last updated in 2020 but it's still there and working perfectly.

@DeadBranches
Copy link

DeadBranches commented Jul 30, 2023

Termux on Google Play is deprecated. Recommend it from F-Droid rather.

No it's not. I'm looking at it on playstore right now. It was was last updated in 2020 but it's still there and working perfectly.

@iomari

depreciate
/dɪˈpriːʃiˌeɪt/
verb
to describe (something) as having little value
- Britannica.com

Termux Google Play

Since November 2, 2020 we no longer able to publish updates of Termux application and add-ons because we are not ready for changes upcoming with SDK level 29 (Android 10).

Everyone should move to F-Droid version, if possible.

- wiki.termux.com

Termux Apps Vulnerability Disclosures

Created: 2022/02/15

This is a vulnerability report for termux-app, termux-tasker and termux-widget.

1. Termux:Tasker Privilege Escalation Vulnerability

This vulnerability allowed execution of any command in termux context or even root context if termux had been granted root permissions by any app.

2. Termux:Widget Privilege Escalation Vulnerability

This vulnerability allowed execution of any command in termux context or even root context

3. Termux Files World Readable

- termux.dev

etc

@sebastianneubert
Copy link

things slightly have changed in android14. but the guide still works with some adjustments. thanks a lot! big kudos.

@Makeshift
Copy link
Author

For those still looking for something like this, if you're able to run it, I highly recommend self-hosting vrtmrz/obsidian-livesync.

Git works, as evidenced by the interest in this, but native sync is really nice

@lead0r
Copy link

lead0r commented Nov 15, 2023

Thank you for this guide. However, I'm getting two errors:

  1. "fatal: detected dubious ownership in repository at..." I tried using the proposed exception (git config --global --add safe.directory /path/to/repo), but I'm still getting this error after a while
  2. Due to using Obsidian on desktop as well, I do not want to push my .obsidian folder, so I added it to .gitignore. However, it somehow still tries to push it
    Has anyone had these issues as well?

@DovieW
Copy link

DovieW commented Nov 15, 2023

  1. If you add it as a safe.directory then it should stay, strange that it would come back.
  2. I wouldn't recommend ignoring your entire .obsidian folder since then all your settings won't be synced and backed up. You can follow my .gitignore that I use for my setup. But the reason why it's still adding it is probably because you are already tracking it. Adding a file to the .gitignore doesn't remove it from being tracked once it was added. You can remove it by using the git rm --cached .obsidian command

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

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