Automatically backup your Claude Code configuration to a private GitHub repo using chezmoi. Your settings, agents, rules, and project configs are version-controlled and synced across machines.
What you get:
- Auto-backup of memory files, rules, settings, agents, and skills
- Auto-push to your dotfiles repo via chezmoi's native
autoPush - Auto-pull on session start to keep multi-machine setups in sync
- Restore your full setup on any machine with two commands
This setup follows the Claude Code memory hierarchy:
| Memory Type | Location | Backed Up |
|---|---|---|
| User memory | ~/.claude/CLAUDE.md |
Yes |
| User rules | ~/.claude/rules/*.md |
Yes |
| Project memory | ./CLAUDE.md or ./.claude/CLAUDE.md |
Yes (if not git-tracked) |
| Project local | ./CLAUDE.local.md |
Yes |
| Project config | ./.claude/ (agents, skills, settings) |
Yes |
Chezmoi manages files at their real paths — no intermediate directories or sync scripts needed.
Global (~/.claude/):
CLAUDE.md— user-level memoryrules/— user-level rules (supports subdirectories)settings.json,settings.local.json— app settings and hooksagents/,plans/— custom agents and plans
Per-project (project/.claude/):
agents/,skills/— project-specific agents and auto-loaded skillssettings.local.json— project hooks and permissionsCLAUDE.local.md— personal project overrides (root level, gitignored)
- chezmoi installed
- A private GitHub repo for your dotfiles
- Claude Code installed
-
Install chezmoi (if not already):
sh -c "$(curl -fsLS get.chezmoi.io)" -
Initialize your dotfiles repo:
chezmoi init your-github-username
-
Enable autoCommit and autoPush — create
~/.config/chezmoi/chezmoi.toml:[git] autoCommit = true autoPush = true
Note: chezmoi does not have a native
autoPull. The SessionStart hook (step 6) handles pulling at session start instead.To track this config in chezmoi itself (since
chezmoi addwon't accept its own config file):cp ~/.config/chezmoi/chezmoi.toml ~/.local/share/chezmoi/.chezmoi.toml cd ~/.local/share/chezmoi && git add .chezmoi.toml && git commit -m "Add chezmoi config" && git push
-
Add your config files to chezmoi:
# Global settings chezmoi add ~/.claude/CLAUDE.md chezmoi add ~/.claude/settings.json chezmoi add ~/.claude/settings.local.json chezmoi add ~/.claude/rules/ # Per-project (repeat for each project) chezmoi add ~/code/my-project/.claude/settings.local.json chezmoi add ~/code/my-project/.claude/agents/ chezmoi add ~/code/my-project/.claude/skills/
-
Add the session sync script — save
chezmoi-session-syncto~/.local/bin/and make it executable:mkdir -p ~/.local/bin # Copy chezmoi-session-sync to ~/.local/bin/chezmoi-session-sync chmod +x ~/.local/bin/chezmoi-session-sync chezmoi add ~/.local/bin/chezmoi-session-sync
-
Add hooks — merge
settings-hooks-snippet.jsoninto~/.claude/settings.local.json(update theSessionStartcommand path to your absolute home directory)
- SessionStart hook: On session start/resume, runs
chezmoi update --forceto pull latest configs from remote — chezmoi hasautoPushbut no nativeautoPull, so this fills the gap - PostToolUse hook: When you edit
.claude/orCLAUDE.mdfiles, runschezmoi add "$file" - Stop hook: When Claude session ends, runs
chezmoi re-addto catch any missed changes - chezmoi autoCommit + autoPush: Automatically commits and pushes after each
chezmoi add - flock: Prevents concurrent backup runs from conflicting
The SessionStart hook uses a small script (~/.local/bin/chezmoi-session-sync) that handles three cases: clean pull (silent), rebase conflict (blocks session with resolution steps), and network failure (warns but doesn't block).
# 1. Clone your projects first (if managing project configs)
git clone https://github.com/you/my-app.git ~/code/my-app
# 2. Install chezmoi and apply
sh -c "$(curl -fsLS get.chezmoi.io)" -- init --apply your-github-usernameThis restores everything: global configs work immediately, project .claude/ folders are placed inside already-cloned repos. The .chezmoi.toml in the repo automatically configures autoCommit and autoPush.
Auto-backup handles everything. For manual operations:
# Add a new file to chezmoi tracking
chezmoi add ~/code/my-project/.claude/agents/new-agent/AGENT.md
# Re-sync all managed files
chezmoi re-add
# Pull latest configs from another machine
chezmoi update~/.claude/debug/,cache/,projects/— session data~/.claude/history.jsonl— command history~/.claude/credentials.json— auth tokens (sensitive)~/.claude/plugins/— downloaded on install, regenerable