- Sharing ideas and telling stories about software systems and startup life/
- Articles/
- The One Where iCloud and Git Stop Fighting Over Your Obsidian Vault/
The One Where iCloud and Git Stop Fighting Over Your Obsidian Vault
git init.Version the Vault Before Handing It to an Agent #
iCloud makes syncing pleasantly uneventful: I write mostly on the MacBook, sometimes write on the phone, and expect the same notes to appear on both.
I also let AI agents work in the vault. I don’t remember what the change was, but I once asked an agent for a small one and didn’t feel confident afterwards, because I had no way to see what it had touched. With the vault in Git, I review the diff before I commit, and when an agent mangles a note, I can get the last committed version back. That is why I’m comfortable giving agents more autonomy.
Keep Git Out of the Synced Folder #
Why the Obvious Setup Is Risky #
I read the warnings about putting a Git repository inside a cloud-synced folder, so I set it up this way from the start. The Git FAQ gives the reason. Git touches many files in the repository during a single operation, and iCloud copies files one at a time without knowing they belong together. When it copies only some of them, the repository can end up with stale lock files or missing objects.
The obvious setup puts all of that inside the vault:
iCloud Drive/
└── Obsidian/
└── MyVault/
├── .obsidian/
├── notes/
└── .git/
├── objects/
├── refs/
├── index
└── logs/
Move the Git Directory Out of iCloud #
Git can keep its database somewhere other than a .git directory beside the checked-out files, which are called the working tree.
It does this with a gitfile: a small .git text file that points to the real Git directory elsewhere.
The vault stays the working tree, and the object database, refs, index and logs live outside iCloud.
git init --separate-git-dir creates this layout.
The result looks like this, with the notes still syncing through iCloud and only Git’s database kept out of it:
iCloud Drive/
└── Obsidian/
└── MyVault/
├── .obsidian/
├── notes/
├── attachments/
└── .git # A small pointer file
~/Git/
└── obsidian-vault.git/
├── objects/
├── refs/
├── index
└── logs/
iCloud still knows nothing about Git, and it could still change a file while Git is reading it. Moving the repository database out of iCloud covers the part those warnings are about, and it has worked reliably for me.
Setting this up on more than one Mac takes extra work, because the pointer file that iCloud syncs holds an absolute path, so each Mac needs its own repository at that same path and they share history through GitHub. I only use one Mac, which keeps this simple.
Create the Backup Repository #
Prepare the Vault #
You can set up the backup from the terminal without installing an Obsidian plugin.
First, keep the vault available offline. Obsidian recommends Keep Downloaded on current macOS versions, and its iCloud guidance explains how. Git can’t read an offloaded file any more than Obsidian can, whatever the repository layout.
In Finder, open iCloud Drive and find the folder with the Obsidian icon.
Open your vault there and check for a note you recognize.
Replace the example path in VAULT with the vault folder’s full pathname from Finder.
Choose a path outside iCloud for VAULT_GIT_DIR and set REPO to the owner and name of your private GitHub repository.
VAULT="$HOME/Library/Mobile Documents/iCloud~md~obsidian/Documents/path/to/your/existing/vault"
VAULT_GIT_DIR="$HOME/Git/obsidian-vault.git"
REPO="OWNER/obsidian-vault"
Decide What Belongs in History #
Back up everything you would want to restore, which includes plugins and settings as well as notes and attachments. Leave out interface state and macOS metadata.
Put a .gitignore like this at the vault root as a starting point:
# Obsidian interface state
.obsidian/workspace.json
.obsidian/workspace-mobile.json
.obsidian/workspaces.json
# Obsidian trash and macOS metadata
.trash/
.DS_Store
Obsidian’s docs describe the workspace files as layout state that changes often, and suggest ignoring them when you manage a vault with Git.
Look through the rest of .obsidian/ before excluding it, because plugin configuration and other settings may be worth keeping in the backup.
Initialize the Separated Repository #
When Git already recognizes the vault, look at its history and remotes, and at the destination path, before you run this. Running
git init --separate-git-diragain moves the existing Git database to the external path and keeps its history and configuration.
# The destination's parent directory must already exist.
mkdir -p "$(dirname "$VAULT_GIT_DIR")"
git -C "$VAULT" init \
--initial-branch=main \
--separate-git-dir="$VAULT_GIT_DIR"
This initializes the real repository at $VAULT_GIT_DIR and writes a .git pointer file at the vault root.
Before the first commit, review what will go into it:
git -C "$VAULT" add -A
git -C "$VAULT" status --short
git -C "$VAULT" diff --cached
git -C "$VAULT" commit -m "Initial backup commit of Obsidian vault"
When the repository has no remote yet, create a private GitHub repository and push:
gh repo create "$REPO" \
--description "Versioned backup of my Obsidian vault." \
--disable-wiki \
--private \
--source="$VAULT" \
--push
The GitHub CLI follows the gitfile when it inspects --source, so the vault behaves like an ordinary local repository even though its database lives elsewhere.
Check That It Worked #
$ file "$VAULT/.git"
.../MyVault/.git: ASCII text
# The .git entry is a small pointer file.
$ git -C "$VAULT" rev-parse --absolute-git-dir
/Users/you/Git/obsidian-vault.git
# The Git directory resolves outside iCloud.
$ git -C "$VAULT" remote -v
origin git@github.com:OWNER/obsidian-vault.git (fetch)
origin git@github.com:OWNER/obsidian-vault.git (push)
$ git -C "$VAULT" status --short
# No output: the working tree is clean.
Give the Setup to an Agent #
Before an agent moves anything, I want it to find out whether the vault already has a Git history.
Read this article first:
https://bytes-of-our-lives.github.io/blog/posts/icloud-git-obsidian-vault/
Inspect my existing Obsidian vault, Git state, ignore rules, and remotes. Do not
modify anything yet.
I want the vault to remain in iCloud while Git's repository database lives
outside iCloud, using Git's documented gitfile mechanism and
`git init --separate-git-dir`.
Before making changes, report:
- the resolved vault path;
- whether Git already recognizes it;
- the current Git directory, if any;
- which files appear to be device-specific interface state;
- the proposed external Git directory;
- whether a remote already exists; and
- the exact migration or initialization plan.
Wait for my approval. Preserve existing history and vault content. If a GitHub
remote must be created, use `gh repo create --private`. After the approved
change, verify that the vault's `.git` is a file, the resolved Git directory is
outside iCloud, the intended files are committed, and the remote is pushed.
Do not switch branches in the iCloud working tree merely to inspect another
revision. Use a linked worktree outside iCloud instead.
Living With It #
When I Commit and Push #
I read and edit notes on either device, and I run Git only on my MacBook. Edits from my iPhone sync to the MacBook through iCloud, so I can commit them there like any other change.
I live in the terminal, so I commit when a coherent set of changes is ready. Sometimes I ask an agent to review the diff and commit it for me. Pushing a commit puts a copy on GitHub.
What I Keep an Eye On #
Four questions tell me the setup is still sound:
- Is the vault kept downloaded, so Git and Obsidian can read every note?
- Does the Git directory live outside iCloud?
- Is my last commit on GitHub?
- Is the working tree clean, so the next diff shows only what the agent did?
Inspect Revisions Without Making iCloud Replay Them #
When I check out an old revision in the live vault, Git can replace many notes at once. iCloud can send those older versions to my iPhone, and send the current ones again when I switch back.
So I use a detached worktree outside iCloud for the checkout. A worktree is a second checked-out folder for the same repository.
# Replace main with any commit, tag, or branch you want to inspect.
git -C "$VAULT" worktree add --detach "$HOME/Git/obsidian-review" main
# The live vault stays untouched; remove the review worktree when finished.
git -C "$VAULT" worktree remove "$HOME/Git/obsidian-review"
When my terminal is already open in the vault, I’m still tempted to switch branches for a quick look. I keep checkouts that could rewrite the whole vault outside the folder iCloud syncs, so I can open an old version of a note on my MacBook without changing the copy on my iPhone.