Obsidian 多端同步实践:基于 Syncthing 与 Git 的分层架构Multi-Device Obsidian Sync: A Layered Architecture with Syncthing and Git

• 约 2 分钟 2 min read

当本地 Markdown 知识库的规模涨到几百篇笔记、并且需要在主力电脑(Windows)和手机(Android)之间随时无缝读写时,单一的同步工具往往很难同时满足**“打开即最新”和“历史可回滚”**这两个目标。

折腾过一圈社区插件后,我最终把“多端实时传输”和“版本控制”拆成了两件正交的事:用 Syncthing(P2P 块级增量同步)承担日常热同步,用 Git 承担桌面端的里程碑快照,两者互不干扰。

三种主流方案的真实体感对比

在定下这套方案前,我对比过三种最常见的同步路径:

方案实时性与速度外部依赖维护心智负担最适合的角色
Syncthing (P2P)秒级块级增量(局域网 20~50 MB/s)零云端依赖(局域网直连 / 打洞 / 自建中继)配置一次后几乎无感日常主力热同步,手机点开即最新
Remotely Save按需或定时轮询依赖 WebDAV 或 S3/OSS 对象存储无需常驻后台 App,全在插件内完成轻量备选,适合不想在手机挂后台进程的人
Obsidian-Git / Termux手动或定时 Pull/Push强依赖 GitHub 连通性手机端解冲突极痛苦,仓库大时易 OOM桌面端版本快照与异地灾备

很多人一开始会试图在手机上也跑 Git 同步,但移动端网络切后台容易中断,且 Android 文件系统对 .git/index.lock 的并发锁支持并不理想,一旦冲突在手机小屏幕上处理起来非常狼狈。把 Git 留在电脑端做“存档点”,而让文件传输回归 P2P,体验立刻顺畅了。

核心配置:让 Syncthing 与 Git 和平共处

用 Syncthing 同步一个同时受 Git 管理的 Obsidian 仓库,最核心的一条铁律是:绝对不要让 Syncthing 同步 .git 目录和设备专属的窗口状态文件。

在 Windows 端(直接以 Headless 模式静默运行 syncthing.exe,通过 127.0.0.1:8384 管理)添加知识库目录时,我在「忽略模式(.stignore)」里固定写入以下规则:

(?d).git
(?d).obsidian/workspace.json
(?d).obsidian/workspace-mobile.json
(?d).obsidian/graph.json
(?d)Desktop.ini
(?d)thumbs.db
(?d).DS_Store

这里有几个细节值得注意:

  • 为什么要忽略 .git? 手机端只需要消费和修改平铺的 .md 与资源文件,不需要知道 Git 对象库的存在。等手机上的修改通过 Syncthing 秒级传回电脑后,在电脑端统一做 git diff 和 git commit,Git 索引永远只在单一桌面文件系统上读写,绝不会损坏。
  • 为什么要忽略 workspace*.json? Obsidian 每次切换标签页或调整侧边栏都会改写 workspace.json,如果把它也纳入同步,电脑和手机每分钟都在互相覆盖布局状态,产生大量无意义的同步冲突。
  • (?d) 前缀的作用:允许 Syncthing 在删除某个目录时,一并清理里面被忽略的临时垃圾文件(如 Desktop.ini),不会因为存在未同步的忽略文件而卡住目录删除。

解决“电脑关机后手机修改无处可去”:24 小时云端中继节点

纯 P2P 同步唯一的短板是要求两台设备至少有一段时间同时在线。如果出门在外电脑已经关机,手机上写的笔记就没法立刻同步出去。

解决办法是在一台轻量云服务器上用 Docker 挂一个 24 小时在线的第三节点,形成 PC ↔ 云服务器 ↔ 手机 的三角拓扑:

docker run -d \
  --name syncthing \
  --restart unless-stopped \
  -p 8384:8384 \
  -p 22000:22000/tcp \
  -p 22000:22000/udp \
  -p 21027:21027/udp \
  -v /data/syncthing/config:/var/syncthing/config \
  -v /data/syncthing/data:/var/syncthing/data \
  syncthing/syncthing:latest

把这台云节点也加入同一个文件夹共享集群后:

  • 在家里连 Wi-Fi 时,电脑和手机直接走局域网高速传输;
  • 在外面且电脑关机时,手机的修改先秒同步到云端节点;等回家电脑一开机,自动从云端拉取差量块,全程无需人工干预。

移动端避坑小结

在 Android 端搭配使用 Syncthing-Fork 时,有三个新手最容易卡住的地方:

  1. 区分“设备 ID”和“文件夹 ID”:扫码配对只是交换了设备公钥(Device ID);真正决定两个目录能否对齐的是 文件夹 ID(Folder ID),两端必须完全一致(注意大小写敏感)。
  2. 手机端的“显式接收滑块”:电脑端勾选共享给手机后,Syncthing-Fork 出于安全默认不会自动拉取,需要点进该文件夹手动打开共享开关并绑定本地路径(如 /storage/emulated/0/Documents/Vault)。
  3. 后台保活权限:务必在系统设置里授予“所有文件管理权限”,并将电池优化设为“无限制”,否则锁屏几分钟后后台套接字就会被系统挂起。

Once a local Markdown knowledge base grows to several hundred notes and needs seamless read/write access across a primary workstation (Windows) and a phone (Android), a single sync tool rarely satisfies both “instant availability on open” and “clean version history”.

After testing the usual community plugins, I decoupled real-time file transport from version control into two orthogonal layers: Syncthing (P2P block-level incremental sync) handles continuous hot sync, while Git handles desktop milestone snapshots.

Comparing the Three Main Approaches

Before settling on this architecture, I evaluated the three most common workflows:

ApproachSpeed & LatencyExternal DependencyMaintenance OverheadBest Role
Syncthing (P2P)Sub-second block-level sync (20–50 MB/s on LAN)Zero cloud lock-in (LAN / hole-punching / private relay)Set-and-forget once configuredPrimary hot sync; notes are already updated when you open the app
Remotely SaveOn-demand or periodic pollingRequires WebDAV or S3/OSS object storageRuns entirely inside the plugin without a background daemonLightweight alternative if you dislike background apps on mobile
Obsidian-Git / TermuxManual or scheduled Pull/PushDepends on GitHub reachabilityPainful merge conflicts on mobile; prone to OOM on large reposDesktop version snapshots & off-site backup

Many users initially try to run Git directly on their phones, only to hit interrupted background network calls, .git/index.lock contention on Android storage, and miserable merge conflict resolution on a small touchscreen. Keeping Git strictly on the desktop as a “save point” while letting P2P handle raw file transport eliminates that friction entirely.

Core Configuration: Making Syncthing and Git Coexist Peacefully

When syncing a Git-tracked Obsidian vault via Syncthing, the golden rule is simple: never let Syncthing sync the .git directory or device-specific workspace layout files.

When adding the vault folder on Windows (running syncthing.exe headlessly and managing it via 127.0.0.1:8384), I always place the following rules in .stignore:

(?d).git
(?d).obsidian/workspace.json
(?d).obsidian/workspace-mobile.json
(?d).obsidian/graph.json
(?d)Desktop.ini
(?d)thumbs.db
(?d).DS_Store

Three details worth noting here:

  • Why ignore .git? Your phone only needs to read and edit flat .md and asset files—it doesn’t need the Git object database. Edits made on mobile sync back to the desktop within seconds via Syncthing, where they show up cleanly in git diff and git commit. Because the .git index is only ever mutated by a single desktop OS, repository corruption is impossible.
  • Why ignore workspace*.json? Obsidian rewrites workspace.json every time you switch tabs or resize a pane. Syncing it causes your desktop and phone to constantly overwrite each other’s UI state and spawn sync conflicts.
  • What does the (?d) prefix do? It tells Syncthing that ignored OS junk files (like Desktop.ini or .DS_Store) can be safely deleted if their parent directory is removed, preventing directory deletions from getting stuck.

Solving the “Sleeping PC” Problem: A 24/7 Cloud Relay Node

The only limitation of pure two-device P2P sync is that both devices must be online at the same time. If your desktop is powered off while you’re away, mobile edits have nowhere to go until you return home.

The fix is running a lightweight 24/7 third node in Docker on a cloud VM, forming a PC ↔ Cloud VM ↔ Phone triangle topology:

docker run -d \
  --name syncthing \
  --restart unless-stopped \
  -p 8384:8384 \
  -p 22000:22000/tcp \
  -p 22000:22000/udp \
  -p 21027:21027/udp \
  -v /data/syncthing/config:/var/syncthing/config \
  -v /data/syncthing/data:/var/syncthing/data \
  syncthing/syncthing:latest

Once this cloud node joins the shared folder cluster:

  • At home on Wi-Fi, the PC and phone transfer blocks directly over the local LAN at maximum speed.
  • On the road with the PC turned off, mobile edits sync immediately to the cloud node; as soon as the PC boots up at home, it pulls the delta blocks automatically.

Mobile Pitfalls to Avoid on Android

When using Syncthing-Fork on Android, three gotchas trip up almost everyone at first:

  1. Device ID vs. Folder ID: Scanning the QR code only exchanges Device IDs. For two folders to actually sync, their Folder ID must match character-for-character (case-sensitive).
  2. Explicit folder opt-in on Android: After sharing a folder from your PC, Syncthing-Fork will not pull files until you open the folder entry on the phone, toggle the share switch for your PC, and bind it to a local directory (e.g., /storage/emulated/0/Documents/Vault).
  3. Background permissions: Grant “All files access” and set battery optimization to “Unrestricted”—otherwise Android will suspend the background socket a few minutes after the screen locks.