将 OpenClaw 工作目录同步到 Git 仓库,实现自动备份和版本控制。
skills/、 __pycache__/、 .clawhub/ 等--force-with-lease 而非 --forcebash
# 同步到指定 Git 仓库
python3 ~/.openclaw/workspace/skills/workspace-git-sync/scripts/sync_workspace.py ~/projects/backup-repo
# 带自定义提交信息
python3 ~/.openclaw/workspace/skills/workspace-git-sync/scripts/sync_workspace.py ~/projects/backup-repo "每日备份"
python
from scripts.sync_workspace import sync_workspace_to_git, quick_sync, force_sync
# 标准同步
result = sync_workspace_to_git("~/projects/backup-repo")
# 快速同步
result = quick_sync("~/projects/backup-repo", "快速备份")
# 强制同步(危险)
result = force_sync("~/projects/backup-repo", "强制覆盖")
参数类型默认值说明target_repo_pathstr必需目标 Git 仓库本地路径branchstr自动检测目标分支commit_msgstr自动生成提交信息exclude_patternslist["skills", ".git", ...]额外排除项pull_before_pushboolTrue推送前是否先 pullstrategystr"rebase"合并策略 (rebase/merge/force)
~/.openclaw/workspace/ 存在git pull --rebase 避免冲突.git/)git add -A → git commit → git push以下文件/目录会自动排除,不会同步:
skills/ — 技能目录(通常很大).git/ — Git 元数据__pycache__/ — Python 缓存.DS_Store — macOS 系统文件node_modules/ — Node.js 依赖.clawhub/ — OpenClaw 缓存text
用户:将 workspace 同步到 ~/backup/openclaw
AI:执行 sync_workspace.py ~/backup/openclaw
python
sync_workspace_to_git(
target_repo_path="~/github-pages",
branch="gh-pages",
commit_msg="Deploy workspace backup"
)
python
# 跳过 pull,直接强制推送
force_sync("~/projects/backup-repo", "Emergency update")
错误场景处理方式源目录不存在报错,检查 ~/.openclaw/workspace/目标不是 Git 仓库报错,提示初始化仓库: git init合并冲突中止操作,提示手动解决无推送权限显示 Git 错误,检查权限或 Token
text
~/.openclaw/workspace/skills/workspace-git-sync/
├── SKILL.md
└── scripts/
└── sync_workspace.py
.git/ 目录~ 展开(如 ~/projects/repo).git/,不会丢失版本历史