Git for Markdown notes: setup, .gitignore and cloud-sync traps

Managing notes7 min read

Put a local Markdown notes folder under Git when you want permanent commit history, word-level diffs, branches for structural rewrites, and an off-machine backup on a private remote. Because every note is plain UTF-8 text, git log -S, git diff --word-diff, and branches work immediately without plugins or proprietary databases. One rule prevents almost every disaster: never initialize .git inside an iCloud Drive, Dropbox, or Nutstore folder, because asynchronous file sync corrupts .git/objects and .git/index mid-write.

What Git gives a Markdown library

Cloud drives mirror the latest state of a folder, including accidental deletions, whereas Git records deliberate checkpoints across the entire tree:

  • Search deleted passages years later. Running git log -S "phrase" -p walks every commit and prints the exact patch that added or removed a sentence, even if the note was later renamed (git log --follow path/to/note.md).
  • Word-level prose diffs. Git's default line diff marks an entire 100-word Markdown paragraph as deleted and re-added whenever you fix one typo. Passing --word-diff (or --color-words) diffs token by token inline so prose edits are actually readable:
git diff --word-diff HEAD~1 -- "Project Plan.md"
  • Branches for major rewrites. Before cutting half an essay or reorganizing a documentation folder, git switch -c restructure gives you a sandbox while main stays intact.
  • Encrypted or private remote backups. Pushing to a private repository on GitHub, GitLab, Codeberg, or a self-hosted Gitea server keeps a complete clone off your laptop.

Why Git and cloud sync corrupt each other

The classic mistake with Git-backed notes is running git init directly inside iCloud Drive/MiaoYan or ~/Dropbox/Notes so the phone gets cloud sync while the Mac gets Git. Sooner or later git status dies with fatal: bad object HEAD or error: short read while indexing.

The failure comes from how Git writes its database. A git commit or git pull --rebase updates multiple files: compressed objects under .git/objects/, .git/index via .git/index.lock, and branch refs under .git/refs/heads/ and .git/HEAD. File-sync clients watch individual files without transaction boundaries, creating three hazards:

  1. Out-of-order partial sync. A cloud client can sync .git/refs/heads/main to a second Mac before the new object under .git/objects/ finishes uploading, leaving the branch pointing at a missing hash.
  2. Evicted placeholder files. When macOS Optimize Mac Storage evicts older iCloud files to .icloud stubs, untouched loose objects inside .git/objects/ vanish from POSIX reads until downloaded, breaking git log and git gc.
  3. Conflicted internal metadata. Opening the folder on two machines produces files like .git/index (conflicted copy) or .git/HEAD 2, splitting repository state.

Keep one owner per folder. If your library lives in ~/Documents/Notes, manage it directly with Git (and open it on iPhone or iPad through a dedicated Git client such as Working Copy). If you rely on iCloud Drive so MiaoYan on Mac, iPhone, and iPad opens the folder natively, keep .git out of iCloud Drive and mirror the plain .md files into a separate local Git repository before committing:

rsync -a --delete \
  --exclude='.git/' \
  --exclude='.DS_Store' \
  --exclude='.miaoyan-conflicts/' \
  "$HOME/Library/Mobile Documents/iCloud~com~tw93~miaoyan/Documents/" \
  "$HOME/Backups/NotesGit/"

Check your exact iCloud folder path in Finder first (App Store builds use MiaoYan's iCloud container, while manual folders sit under com~apple~CloudDocs). More on cloud-drive boundaries is in Sync Markdown notes across devices.

Configure Git for Chinese filenames and MiaoYan folders

Initialize the repository in a local folder and set three repository-level (or --global) options right away:

cd ~/Documents/Notes
git init
git config core.quotepath false
git config pull.rebase true
git config rebase.autoStash true
  • core.quotepath false stops Git from octal-escaping bytes above 0x80. With Git's default (true), a file named 读书笔记.md prints in git status as "\350\257\273\344\271\246\347\254\224\350\256\260.md". Setting it to false prints UTF-8 Chinese, Japanese, Korean, and accented filenames directly.
  • pull.rebase true and rebase.autoStash true keep history linear across two Macs. If a note in your working tree has uncommitted edits when you pull, Git stashes those edits automatically, rebases local commits onto the remote tip without noisy merge commits, and reapplies your working tree changes.

Next, add a .gitignore at the root of the notes folder:

.DS_Store
.miaoyan-conflicts/
Trash/

Each entry matches how MiaoYan lays out a library on disk:

  • .DS_Store ignores macOS Finder folder view metadata.
  • .miaoyan-conflicts/ is the hidden safety directory MiaoYan creates at the library root if an external tool updates an open note on disk while you have unsaved edits in the editor (keeping the 100 newest backups and anything under 30 days).
  • Trash/ is MiaoYan's library-level Trash directory where deleted notes wait before permanent removal. Ignoring Trash/ keeps deleted drafts out of new commits; omit that line if you prefer deleted notes tracked until you empty Trash.

Watch binary growth in i/ and files/. MiaoYan stores pasted images in i/ and dragged videos or PDFs in files/ beside your notes. Plain Markdown text takes kilobytes per year, whereas fifty raw screen recordings in files/ swell .git/objects/pack forever even if you delete them later. Run MiaoYan's Clean Unused Attachments before committing, upload heavy images directly to S3 or Cloudflare R2 if you publish notes online, or track *.mov and *.mp4 with Git LFS.

A safe commit-and-sync script

Avoid cron jobs that commit every sixty seconds. Minute-by-minute auto-commits slice half-typed sentences into thousands of useless log entries and can collide with editor saves. Run a small script on demand at the end of a writing session (for example as a notes-sync shell command alongside the miao CLI covered in Markdown notes from the terminal):

#!/usr/bin/env bash
set -euo pipefail

cd "${HOME}/Documents/Notes"

if git remote get-url origin >/dev/null 2>&1; then
  git pull --rebase --autostash --quiet origin main
fi

git add -A
if git diff --cached --quiet; then
  exit 0
fi

changed=$(git diff --cached --numstat | wc -l | tr -d ' ')
git commit -m "notes: $(date '+%Y-%m-%d %H:%M') (${changed} files)" --quiet

if git remote get-url origin >/dev/null 2>&1; then
  git push --quiet origin main
fi

git diff --cached --quiet exits cleanly when nothing changed, and pull --rebase --autostash brings in remote commits before recording the new snapshot.

How MiaoYan Version History and Git work together

Git and MiaoYan's built-in Version History (⇧⌘H) cover different time scales. While you write, MiaoYan saves snapshots automatically every five minutes of editing and on every note switch, keeping up to 20 versions per note under ~/Library/Application Support/com.tw93.miaoyan/Versions/ (capped at 200 MB across the library since V4.12.0). Each row shows +N -M line changes and highlights added lines green and rewritten lines yellow. Because snapshots live in Application Support outside your notes folder, they leave git status clean while rescuing paragraphs deleted ten minutes ago before any commit; see Markdown note version history.

Git handles everything past 20 edits: permanent milestones, versioned i/ diagrams, cross-note history searches, and off-site clones. When git switch or git pull adds, renames, or deletes .md files on disk, MiaoYan's file watcher updates the sidebar and note list immediately. MiaoYan is on the App Store for Mac, iPhone, and iPad, with the Mac source on GitHub.

FAQ

How do I recover a Git repository corrupted by iCloud Drive or Dropbox?

Copy the folder to a local path such as ~/Documents/NotesRecovery, download every .icloud placeholder, and run git fsck --full. If objects are missing and you pushed to a remote earlier, clone fresh outside any cloud folder and copy over your latest .md files.

Why does git diff mark a whole paragraph as one changed line?

Soft-wrapped Markdown paragraphs sit on a single line, and Git diffs line by line by default. Run git diff --word-diff to highlight inline insertions ({+added+}) and deletions ([-removed-]).

Will MiaoYan show .git in the sidebar?

No. MiaoYan skips dot-prefixed hidden directories when scanning folders, hiding .git/, .github/, and .miaoyan-conflicts/.

Should I commit i/ image folders into Git?

Yes for ordinary screenshots and diagrams, keeping notes self-contained across clones. Reserve Git LFS or an S3 / R2 bucket for large videos in files/ or hundreds of heavy photos.