Introduction
I’ve tried many tools and tutorials to sync my obsidian notes between android and windows. They all have the same problems:
- Some need subscription service (Obsidian Sync) which I’m not willing to pay
- Some can’t handle editing the same file (Sync via GoogleDrive, Dropbox and …)
- Others need complicated setup and maintenance.
My Solution
This solution takes some time (at most 10 minutes) to setup. But, once it is finished, it works perfectly. I have tested this solution since 2012 (roughly a year). So, there shouldn’t be any problem once you set it up.
Initial Setup
- Create a Github or Gitlab Repository (Make sure it is
private
) - Create SSH-Key and add it to your
ssh-agent
- Clone your repository to a place in your device
- Open that folder as a vault in obsidian
- You can also move your current obsidian notes to this folder (in case you have any).
Creating SSH-Key for Cloning Git Repo
If you don’t want to read github documentation, here’s how to do it quickly.
First, you create an ssh-key using:
cd ~
ssh-keygen -t ed25519 -C "[email protected]"
Press Enter if you see any messages. It will ask about where to store the file and also the password. It doesn’t matter. Then Enter this command to see your public key:
cat ~/.ssh/id_ed25519.pub
Copy all the output into your clipboard. Then login to your Github account and Go to Settings -> SSH and GPG keys -> Add ssh-key
. Then add your ssh key to your account.
You can now go into your repository and copy your ssh url and then use it for cloning.
Sync Windows and Mac
For windows and mac, you can just install obsidian-git
plugin. Installing that would be a breeze. Just make sure to set automatic pull and push (I’ve set it to 5 minutes).
I’ve pinned these three commands since sometimes I want to instantly sync my changes. Obsidian > Options > Command Pallete > Pinned Commands
- Obsidian Git: Commit all changes
- Obsidian Git: Push
- Obsidian Git: Pull
GitHub - denolehov/obsidian-git: Backup your Obsidian.md vault with git Also, here is the repo if you want to check it out.
Update 2023-10-08
This blog have gotten a lot of attention. So, I decided to simplify things and write a bash scripts which does all these stuff pretty automatically.
I have written my script in this repository. To follow along just copy this line and go with video tutorial. This way it only takes five minutes to complete the setup.
bash -c "$(curl -fsSL https://raw.githubusercontent.com/pourmand1376/obsidian-sync/main/obsidian.sh)"
YoutubeVideo Tutorial Part 1:
Youtube Video Tutorial Part 2:
Old Version
I keep the old version of the blog so that you can see how I would use the system previously.
But fear not. Just use the script above and It would handle everything written down here automatically.
ExtraOption for windows users
Note that if you are using windows, you should also use this command to prevent path error on odd filenames (+)
git config core.protectNTFS false
Automatic Merging
Sometimes you may change the same file. I know it is rare but it is possible. We prevent conflict by two ways (gitignore and gitattributes).
These files need to be created at the root of your directory (where .git
folder resides).
touch .gitignore
touch .gitattributes
First, You should add these lines to .gitignore
:
.trash/
.obsidian/workspace
.obsidian/workspace.json
.obsidian/workspace-mobile.json
.obsidian/app.json
This tells git to ignore certain files that are likely to cause conflicts, like recently opened files.
In case you have previously committed workspace.json
and other mentioned gitignore files into git, you should remove them using the following commands (If you are not sure, it wouldn’t hurt to run these commands):
git rm --cached .obsidian/workspace
git rm --cached .obsidian/workspace.json
git rm --cached .obsidian/workspace-mobile.json
git rm --cached .obsidian/app.json
git commit -am "Remove gitignore files"
Sync Android
For this part, I used this tutorial for the reference:
Here I assume that you have a fully working obsidian in your windows (or mac). If not, please first sync your windows (or mac) to Git and then follow the rest of this tutorial.
To Use Git on Android:
- First, you’ll need to download and install the Termux app from the Google Play Store (Termux – Apps on Google Play). Once you have it installed, open the app and run the following commands
apt update
apt upgrade -y
pkg install openssh -y
pkg install git -y
termux-setup-storage
These commands will update your Termux installation, install the necessary packages for Git, and set up your storage so you can access your files.
- Next, you’ll need to add your Git configurations. Run the following commands:
git config --global user.email "<your_email>"
git config --global user.name "<The name you want on your commits>"
git config --global credential.helper store
git config --global pull.rebase false
git config --global --add safe.directory '*'
Replace <your_email>
and <The name you want on your commits>
with your own information.
- Now you’re ready to clone a repository. Use the following command (make sure to replace your repository url):
cd ~
mkdir obsidian
git --git-dir ~/obsidian --work-tree ~/storage/downloads/obsidian clone <your repository_github_url>
Note that here we are separting git working directory and bare git directory. This is a must and the reason for taking this approach is explained here.
For Authentication, you have two ways. Use Personal Access Tokens, or use SSH. I use the second as the first one is blocked in my country.
You should now have the repository folder on your device and you should be able to open your obsidian files inside its app. If you haven’t changes my commands, you should find your obsidian vault in downloads/obsidian
folder.
Here, You should go to Settings -> Community Plugins -> Obsidian Git and make sure
Disable on this device
is checked (unfortunately this plugin doesn’t work well on mobile).
But how do we sync obsidian? Bare with me.
The first step is to add a .profile
file to your Termux home directory. To do this, run the following commands:
cd ~
touch ~/.profile
echo "source ~/.profile" >> ~/.bashrc
echo 'function sync_obsidian
{
cd ~/obsidian
git add .
git commit -m "Android Commit"
git fetch
git merge --no-edit
git add .
git commit -m "automerge android"
git push
echo "Sync is finished"
sleep 2
}
alias ob="sync_obsidian"
alias o="sync_obsidian && exit"
alias 0="o"' > ~/.profile
These lines will create a function called sync_obsidian
inside .profile
that will take care of syncing your notes.
That’s all. You can now sync your obsidian vault using 0
or o
alias in termux (Just type 0 and hit enter).
It’s important not to use
git pull
insync_obsidian
function since it will prompt you for a merge commit message every time there are conflicts, which can be very annoying. Why is git prompting me for a post-pull merge commit message? - Stack Overflow
Here is also a short video to see how this works inside Termux.
Note for iPhone Users
I do not own an iPhone. But one of my friends does and he followed this tutorial successfully.
Only a few notes here:
- Apparently, there is no need to clone the way I said in android. Clone it normally.
- I think my script for syncing obsidian is much more robust and more importantly it makes you conflict-proof!
Good Luck!