Git Gogs 代码自动同步方案 - 多方案自动同步脚本与配置说明
|
|
2 dní pred | |
|---|---|---|
| Git代码自动同步详细说明.md | 2 dní pred | |
| README.md | 2 dní pred |
多种方案实现本地代码与 Gogs 仓库的自动同步,减少手动操作
最后更新: 2026-10-08
Gogs 仓库: https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync
| 方案 | 适用场景 | 实时性 |
|---|---|---|
| Git Hook 自动 Push | 本地提交后自动推送,单人多设备 | 提交即同步 |
| 定时 Cron/任务计划 | 定时双向同步,服务器↔开发机 | 分钟级 |
| 文件监听(inotify/fswatch) | 保存即同步,无需等待 | 秒级实时 |
| Git Bundle 离线同步 | 不联网环境,U盘介质传输 | 手动触发 |
| rsync + Git 混合 | 大文件+代码一起同步 | 分钟级 |
Git代码自动同步详细说明.md — 完整技术方案文档,包含所有方案的实现步骤、脚本示例和注意事项README.md — 本文件,项目概览cd /path/to/your/repo
# 创建 post-commit hook
cat > .git/hooks/post-commit << 'HOOK'
#!/bin/bash
git push origin $(git branch --show-current) 2>&1 | tee -a .git/hooks/push.log
HOOK
chmod +x .git/hooks/post-commit
# 每 5 分钟自动 pull + push
*/5 * * * * cd /path/to/repo && git pull --rebase origin main && git push origin main >> /var/log/git-sync.log 2>&1
# 需要 inotify-tools
inotifywait -m -r -e close_write,move,create,delete /path/to/repo |
while read dir events file; do
cd /path/to/repo
git add -A
git diff --cached --quiet || {
git commit -m "auto sync: $(date '+%Y-%m-%d %H:%M:%S')"
git push origin main
}
done
ssh -T git@git.zhonjin.com 能通过认证git config user.name 和 user.email 已设置.gitignoreGogs 服务器: https://git.zhonjin.com:40717