Prechádzať zdrojové kódy

docs: 新增 Git Gogs 完整操作手册(Web界面/CLI/API/脚本/备份全覆盖)

steven_roc 2 dní pred
commit
e58b3beb66

+ 276 - 0
Git_Gogs代码下载完整指南.md

@@ -0,0 +1,276 @@
+# Git Gogs 代码下载完整指南
+
+> 仓库地址: https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync  
+> 最后更新: 2026-10-08
+
+---
+
+## 一、当前仓库状态
+
+```
+最新提交: ddf5039 init: 初始化项目,添加 README 和自动同步方案文档
+分支: main
+文件:
+  ├── README.md
+  └── Git代码自动同步详细说明.md
+```
+
+---
+
+## 二、下载方式总览
+
+| 方式 | 命令/操作 | 适用场景 | 是否需要 Git |
+|------|-----------|----------|-------------|
+| **git clone(HTTPS)** | `git clone <url>` | 日常开发,需后续修改和推送 | ✅ 需要 |
+| **git clone(SSH)** | `git clone git@...` | 已配置 SSH Key 的开发机 | ✅ 需要 |
+| **下载 ZIP 归档** | 浏览器点击下载 | 快速查看,不需要提交 | ❌ 不需要 |
+| **API 下载** | `curl` + Access Token | 服务器/脚本自动化 | ❌ 不需要 |
+| **wget 直接下载** | `wget` 指定分支/标签 | CI/CD 流水线 | ❌ 不需要 |
+
+---
+
+## 三、方式 1:git clone(HTTPS)—— 最常用
+
+### 3.1 基本克隆
+
+```bash
+# 克隆整个仓库(默认拉取 main 分支)
+git clone https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+
+# 克隆到指定目录
+git clone https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git my-project
+
+# 克隆时指定分支
+git clone -b main https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+```
+
+### 3.2 认证方式
+
+**方式 A:URL 中嵌入用户名密码**
+```bash
+git clone https://steven_roc:你的密码@git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+```
+
+**方式 B:URL 中嵌入 Access Token**
+```bash
+git clone https://steven_roc:你的Access_Token@git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+```
+
+**方式 C:使用 Git Credential 缓存(推荐,不用每次输入)**
+```bash
+# 首次克隆时会提示输入用户名和密码(填 Access Token)
+git clone https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+
+# 缓存凭证 1 小时
+git config --global credential.helper 'cache --timeout=3600'
+
+# 或永久存储(写入 ~/.git-credentials)
+git config --global credential.helper store
+```
+
+### 3.3 克隆后目录结构
+
+```
+git_gog_code_sync/
+├── .git/                          # Git 元数据
+├── README.md                      # 项目说明
+└── Git代码自动同步详细说明.md       # 完整技术方案
+```
+
+---
+
+## 四、方式 2:git clone(SSH)
+
+### 4.1 前提:配置 SSH Key
+
+```bash
+# 生成密钥(如果还没有)
+ssh-keygen -t ed25519 -C "steven_roc@zhonjin.com"
+
+# 查看公钥并添加到 Gogs
+cat ~/.ssh/id_ed25519.pub
+# → 登录 Gogs → 设置 → SSH 密钥 → 添加密钥
+
+# 测试连接
+ssh -T -p 40717 git@git.zhonjin.com
+```
+
+### 4.2 SSH 克隆
+
+```bash
+git clone git@git.zhonjin.com:steven_roc/git_gog_code_sync.git
+```
+
+---
+
+## 五、方式 3:浏览器下载 ZIP
+
+### 5.1 操作步骤
+
+1. 打开浏览器访问:https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync
+2. 点击分支选择器(默认显示 `main`)
+3. 点击 **「下载存档」** 按钮
+4. 选择 `ZIP` 格式下载
+
+### 5.2 直接下载链接
+
+```
+# main 分支 ZIP
+https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync/archive/main.zip
+
+# main 分支 TAR.GZ
+https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync/archive/main.tar.gz
+```
+
+> ⚠️ 浏览器访问需要登录 Gogs 账号
+
+---
+
+## 六、方式 4:API + Access Token 下载
+
+### 6.1 下载 ZIP 归档
+
+```bash
+# 使用 Access Token 下载 main 分支 ZIP
+curl -sk -o git_gog_code_sync.zip \
+  -H "Authorization: token 你的Access_Token" \
+  "https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync/archive/main.zip"
+
+# 解压
+unzip git_gog_code_sync.zip
+```
+
+### 6.2 通过 API 获取文件列表
+
+```bash
+# 查看仓库根目录文件列表
+curl -sk -H "Authorization: token 你的Access_Token" \
+  "https://git.zhonjin.com:40717/api/v1/repos/steven_roc/git_gog_code_sync/contents/"
+
+# 获取某个文件内容(base64 编码)
+curl -sk -H "Authorization: token 你的Access_Token" \
+  "https://git.zhonjin.com:40717/api/v1/repos/steven_roc/git_gog_code_sync/contents/README.md"
+```
+
+### 6.3 获取指定 commit 的代码
+
+```bash
+# 先查看提交历史,获取 commit SHA
+curl -sk -H "Authorization: token 你的Access_Token" \
+  "https://git.zhonjin.com:40717/api/v1/repos/steven_roc/git_gog_code_sync/commits?sha=main&limit=5"
+
+# 下载指定 commit 的代码
+curl -sk -o specific_commit.zip \
+  -H "Authorization: token 你的Access_Token" \
+  "https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync/archive/<commit_sha>.zip"
+```
+
+---
+
+## 七、方式 5:wget 直接下载
+
+```bash
+# 下载 main 分支 ZIP(需要认证)
+wget --no-check-certificate \
+  --user=steven_roc --password=你的Access_Token \
+  -O git_gog_code_sync.zip \
+  "https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync/archive/main.zip"
+```
+
+---
+
+## 八、克隆后获取最新代码
+
+克隆完成后,后续更新到最新代码:
+
+```bash
+cd git_gog_code_sync
+
+# 拉取最新代码(推荐 rebase 方式,保持提交历史干净)
+git pull --rebase origin main
+
+# 或传统 merge 方式
+git pull origin main
+
+# 查看更新日志
+git log --oneline -5
+```
+
+---
+
+## 九、下载指定版本/标签
+
+```bash
+# 查看所有标签
+git ls-remote --tags https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+
+# 克隆并切换到特定标签
+git clone https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+cd git_gog_code_sync
+git checkout v1.0.0
+
+# 或一步到位
+git clone --branch v1.0.0 --depth 1 \
+  https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+```
+
+---
+
+## 十、浅克隆(加速下载大仓库)
+
+```bash
+# 只下载最新 1 次提交(速度快,适合只看最新代码)
+git clone --depth 1 https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+
+# 如果后续需要完整历史
+cd git_gog_code_sync
+git fetch --unshallow
+```
+
+---
+
+## 十一、各场景推荐方案
+
+| 场景 | 推荐方式 | 命令 |
+|------|----------|------|
+| 开发机日常开发 | git clone HTTPS | `git clone <url>` |
+| 树莓派/服务器 | git clone SSH | `git clone git@...` |
+| 快速查看代码 | 浏览器下载 ZIP | 直接访问仓库页 |
+| CI/CD 自动化 | API + Token | `curl -H "Authorization: token ..."` |
+| 只看最新代码 | 浅克隆 | `git clone --depth 1` |
+| 下载特定历史版本 | API 获取 commit SHA | `archive/<sha>.zip` |
+
+---
+
+## 十二、常见问题
+
+### Q: 克隆时提示 SSL 证书错误?
+```bash
+# 临时跳过 SSL 验证(自签名证书场景)
+GIT_SSL_NO_VERIFY=true git clone https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+
+# 或永久配置(不推荐,仅内网环境使用)
+git config --global http.sslVerify false
+```
+
+### Q: 克隆后看不到其他分支?
+```bash
+# 查看所有远程分支
+git branch -r
+
+# 切换到远程的某个分支
+git checkout -b local-branch origin/remote-branch
+```
+
+### Q: 下载速度太慢?
+```bash
+# 使用浅克隆减少数据量
+git clone --depth 1 --single-branch <url>
+
+# 或使用 SSH(通常比 HTTPS 快)
+git clone git@git.zhonjin.com:steven_roc/git_gog_code_sync.git
+```
+
+---
+
+*文档维护: 每次下载方式变更时更新本文档*

+ 484 - 0
Git_Gogs完整操作手册.md

@@ -0,0 +1,484 @@
+# Git Gogs 完整操作手册
+
+> Gogs 服务器: https://git.zhonjin.com:40717  
+> 用户: steven_roc  
+> 最后更新: 2026-10-08
+
+---
+
+## 一、当前所有仓库总览
+
+| 仓库 | 说明 | 大小 | 最近更新 |
+|------|------|------|----------|
+| git_gog_code_sync | Git Gogs 代码自动同步方案 | 28 MB | 2026-10-08 |
+| hikvision-isup-client | 海康 ISUP 客户端 C 项目 | 372 MB | 2026-10-08 |
+| dts_monitor_android | DTS 光纤温度监控 Android 客户端 | 348 MB | 2026-10-07 |
+| mppt_monitor | MPPT 监控 | 34.6 GB | 2026-10-07 |
+| mppt_monitor_android | MPPT 监控 Android 客户端 | 248 MB | 2026-10-07 |
+| android-client | 宝林电厂环流监控 Android 客户端 | 160 MB | 2026-10-07 |
+| md5-keygen | CPU序列号MD5密钥 Web 服务 | 1.08 GB | 2026-10-07 |
+| md5-keygen-android | CPU序列号MD5密钥 Android App | 104 MB | 2026-10-07 |
+| nginx-stream-ssl-preread | Nginx Stream + SSL Preread 分流 | 80 MB | 2026-10-06 |
+| PlateRecApp | 车牌识别系统 | 483 MB | 2026-10-05 |
+| current-monitor | 宝林电厂环流监控系统 | 7.6 GB | 2026-10-05 |
+| dts_monitor | DTS 分布式光纤温度传感器监控 | 180 MB | 2026-10-05 |
+
+---
+
+## 二、Gogs Web 界面完整操作指南
+
+### 2.1 访问仓库主页
+
+浏览器打开:`https://git.zhonjin.com:40717/steven_roc/<仓库名>`
+
+例如:
+- https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync
+- https://git.zhonjin.com:40717/steven_roc/PlateRecApp
+
+### 2.2 查看所有提交历史
+
+**路径**: 仓库页面 → 点击 **「提交数」** 或 **「Commits」** 链接
+
+URL 格式:
+```
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/commits/master
+```
+
+**操作说明**:
+- 每一条提交显示:提交信息、SHA(前8位)、提交时间、作者
+- 点击 **SHA 编号** → 查看该次提交的完整 diff(改了哪些文件的哪些行)
+- 点击 **提交信息** → 查看该次提交的详情
+- 点击 **浏览代码** → 查看该次提交时的完整文件快照(🔑 这就是查看上一次版本的方法)
+
+### 2.3 🔑 查看上一次版本的完整内容
+
+**方法 A:通过提交历史回退查看**
+
+1. 进入仓库页面 → 点击 **「提交数」**
+2. 找到你想查看的那次提交(比如上一次推送)
+3. 点击该提交右侧的 **「<>」浏览代码按钮**
+4. 此时看到的就是该次提交时的完整文件列表和内容
+
+**方法 B:通过切换标签/分支查看**
+
+如果有打 Tag:
+1. 仓库页面 → 点击 **「标签」** 或 **「Tags」**
+2. 点击具体标签名 → 查看该版本的完整代码
+
+**方法 C:通过 URL 直接访问指定版本**
+
+```
+# 查看指定 commit 时的文件列表
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/src/<commit_sha>
+
+# 查看指定 commit 时的某个文件
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/src/<commit_sha>/<文件路径>
+```
+
+**方法 D:对比两个版本之间的差异**
+
+```
+# 对比两次提交的差异
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/compare/<旧commit_sha>...<新commit_sha>
+```
+
+### 2.4 在 Web 上查看单个文件
+
+```
+# 查看文件内容(带语法高亮)
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/src/master/<文件路径>
+
+# 查看文件原始内容(纯文本,可直接复制)
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/raw/master/<文件路径>
+
+# 查看文件的提交历史(这个文件被改了哪几次)
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/commits/master/<文件路径>
+
+# 查看某个文件在某个版本的内容
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/raw/<commit_sha>/<文件路径>
+```
+
+### 2.5 在 Web 上下载代码
+
+| 下载方式 | 操作 |
+|----------|------|
+| **ZIP 归档** | 仓库页 → 点击 **「克隆/下载」** → **「下载 ZIP」** |
+| **指定版本 ZIP** | 仓库页 → **「提交数」** → 某次提交 → **「<>」** → **「下载 ZIP」** |
+| **指定分支/标签** | 仓库页 → 分支选择器切换到目标分支/标签 → **「下载 ZIP」** |
+
+直接下载链接:
+```
+# 最新 master 分支
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/archive/master.zip
+
+# 指定 commit
+https://git.zhonjin.com:40717/steven_roc/<仓库名>/archive/<commit_sha>.zip
+```
+
+### 2.6 Web 界面功能速查
+
+| 功能 | 入口 |
+|------|------|
+| 📁 浏览文件 | 仓库主页 |
+| 📝 查看提交历史 | **「提交数」** 链接 |
+| 🔍 搜索代码 | 仓库页搜索框 |
+| 📊 查看文件修改历史 | 文件页 → **「历史」** |
+| 🔀 查看分支列表 | **「分支」** 下拉菜单 |
+| 🏷️ 查看标签/版本 | **「标签」** 链接 |
+| 📥 下载归档 | **「克隆/下载」** → **「下载 ZIP」** |
+| ⚙️ 仓库设置 | **「设置」** 选项卡 |
+
+---
+
+## 三、Git 命令行完整操作
+
+### 3.1 首次克隆
+
+```bash
+# HTTPS 克隆
+git clone https://git.zhonjin.com:40717/steven_roc/<仓库名>.git
+cd <仓库名>
+
+# SSH 克隆(需先配置 SSH Key)
+git clone git@git.zhonjin.com:steven_roc/<仓库名>.git
+
+# 浅克隆(只拉最新一次提交,速度快)
+git clone --depth 1 https://git.zhonjin.com:40717/steven_roc/<仓库名>.git
+```
+
+### 3.2 查看历史版本
+
+```bash
+# 查看提交历史
+git log --oneline
+
+# 查看某个 commit 的详细改动
+git show <commit_sha>
+
+# 查看某个 commit 时的某个文件内容
+git show <commit_sha>:<文件路径>
+
+# 回退到上一个版本(保留改动在工作区)
+git reset --soft HEAD~1
+
+# 回退到上一个版本(彻底丢弃改动)
+git reset --hard HEAD~1
+
+# 切换到某个历史版本查看(不改变当前分支)
+git checkout <commit_sha>
+
+# 回到最新
+git checkout master
+```
+
+### 3.3 日常修改推送
+
+```bash
+# 1. 拉取最新代码
+git pull origin master
+
+# 2. 查看改了什么
+git status
+git diff
+
+# 3. 添加并提交
+git add -A
+git commit -m "描述你改了什么"
+
+# 4. 推送到 Gogs
+git push origin master
+```
+
+### 3.4 分支管理
+
+```bash
+# 创建并切换到新分支
+git checkout -b feature-xxx
+
+# 推送新分支到远程
+git push -u origin feature-xxx
+
+# 合并分支到 master
+git checkout master
+git merge feature-xxx
+
+# 删除远程分支
+git push origin --delete feature-xxx
+```
+
+### 3.5 标签管理(版本发布)
+
+```bash
+# 创建标签
+git tag -a v1.0.0 -m "版本 1.0.0 发布"
+
+# 推送标签到 Gogs
+git push origin v1.0.0
+
+# 推送所有标签
+git push origin --tags
+
+# 查看所有标签
+git tag -l
+
+# 切换到某个标签
+git checkout v1.0.0
+```
+
+---
+
+## 四、Gogs API 完整参考
+
+### 4.1 认证方式
+
+所有 API 请求都需要 Access Token:
+```bash
+# Header 方式(推荐)
+curl -H "Authorization: token <你的Access_Token>" <URL>
+
+# URL 参数方式
+curl "<URL>?token=<你的Access_Token>"
+```
+
+### 4.2 仓库相关 API
+
+```bash
+BASE="https://git.zhonjin.com:40717/api/v1"
+
+# 列出所有仓库
+curl -sk -H "Authorization: token $TOKEN" "$BASE/user/repos"
+
+# 创建仓库
+curl -sk -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \
+  -d '{"name":"新仓库名","description":"描述","private":false}' \
+  "$BASE/user/repos"
+
+# 获取仓库信息
+curl -sk -H "Authorization: token $TOKEN" "$BASE/repos/steven_roc/<仓库名>"
+
+# 删除仓库
+curl -sk -X DELETE -H "Authorization: token $TOKEN" "$BASE/repos/steven_roc/<仓库名>"
+```
+
+### 4.3 文件相关 API
+
+```bash
+# 获取目录文件列表
+curl -sk -H "Authorization: token $TOKEN" \
+  "$BASE/repos/steven_roc/<仓库名>/contents/"
+
+# 获取文件内容(base64 编码)
+curl -sk -H "Authorization: token $TOKEN" \
+  "$BASE/repos/steven_roc/<仓库名>/contents/README.md"
+
+# 获取文件原始内容(纯文本)
+curl -sk -H "Authorization: token $TOKEN" \
+  "https://git.zhonjin.com:40717/steven_roc/<仓库名>/raw/master/README.md"
+
+# 创建/更新文件
+curl -sk -X PUT -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \
+  -d '{
+    "message": "update README",
+    "content": "'$(base64 -w0 README.md)'"
+  }' \
+  "$BASE/repos/steven_roc/<仓库名>/contents/README.md"
+
+# 删除文件
+curl -sk -X DELETE -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \
+  -d '{"message":"delete file","sha":"<文件sha>"}' \
+  "$BASE/repos/steven_roc/<仓库名>/contents/<文件路径>"
+```
+
+### 4.4 提交历史 API
+
+```bash
+# 查看提交列表
+curl -sk -H "Authorization: token $TOKEN" \
+  "$BASE/repos/steven_roc/<仓库名>/commits?sha=master&limit=10"
+
+# 查看单个提交详情
+curl -sk -H "Authorization: token $TOKEN" \
+  "$BASE/repos/steven_roc/<仓库名>/git/commits/<commit_sha>"
+
+# 查看某个提交的 diff
+curl -sk -H "Authorization: token $TOKEN" \
+  "$BASE/repos/steven_roc/<仓库名>/commits/<commit_sha>/diff"
+```
+
+### 4.5 分支与标签 API
+
+```bash
+# 查看所有分支
+curl -sk -H "Authorization: token $TOKEN" \
+  "$BASE/repos/steven_roc/<仓库名>/branches"
+
+# 查看所有标签
+curl -sk -H "Authorization: token $TOKEN" \
+  "$BASE/repos/steven_roc/<仓库名>/tags"
+
+# 创建标签
+curl -sk -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \
+  -d '{"tag_name":"v1.0.0","message":"版本1.0","target":"master"}' \
+  "$BASE/repos/steven_roc/<仓库名>/tags"
+```
+
+### 4.6 下载归档 API
+
+```bash
+# 下载指定分支 ZIP
+curl -sk -H "Authorization: token $TOKEN" \
+  -o repo.zip \
+  "https://git.zhonjin.com:40717/steven_roc/<仓库名>/archive/master.zip"
+
+# 下载指定标签 ZIP
+curl -sk -H "Authorization: token $TOKEN" \
+  -o repo-v1.zip \
+  "https://git.zhonjin.com:40717/steven_roc/<仓库名>/archive/v1.0.0.zip"
+
+# 下载指定 commit ZIP
+curl -sk -H "Authorization: token $TOKEN" \
+  -o repo-specific.zip \
+  "https://git.zhonjin.com:40717/steven_roc/<仓库名>/archive/<commit_sha>.zip"
+```
+
+---
+
+## 五、自动同步脚本(生产级)
+
+### 5.1 完整推送脚本
+
+```bash
+#!/bin/bash
+# git_gogs_push.sh - 完整推送脚本
+# 用法: ./git_gogs_push.sh [提交说明]
+
+set -euo pipefail
+
+COMMIT_MSG="${1:-auto sync: $(date '+%Y-%m-%d %H:%M:%S')}"
+GOGS_URL="https://git.zhonjin.com:40717"
+REPO_DIR="$(cd "$(dirname "$0")" && pwd)"
+
+cd "$REPO_DIR"
+
+# 检查是否有变更
+if [[ -z "$(git status --porcelain)" ]]; then
+    echo "✅ 没有变更,跳过推送"
+    exit 0
+fi
+
+# 显示变更内容
+echo "📋 检测到以下变更:"
+git status --short
+
+# 添加、提交、推送
+git add -A
+git commit -m "$COMMIT_MSG"
+git push origin master
+
+echo "✅ 推送完成: $COMMIT_MSG"
+echo "🔗 仓库: $GOGS_URL/steven_roc/$(basename "$REPO_DIR")"
+```
+
+### 5.2 完整拉取脚本
+
+```bash
+#!/bin/bash
+# git_gogs_pull.sh - 完整拉取脚本
+# 用法: ./git_gogs_pull.sh
+
+set -euo pipefail
+
+REPO_DIR="$(cd "$(dirname "$0")" && pwd)"
+cd "$REPO_DIR"
+
+echo "📥 拉取最新代码..."
+git pull --rebase origin master
+
+echo ""
+echo "📋 最近 5 次提交:"
+git log --oneline -5
+
+echo ""
+echo "📁 当前文件:"
+ls -la
+```
+
+### 5.3 一键备份全部仓库脚本
+
+```bash
+#!/bin/bash
+# gogs_backup_all.sh - 备份 Gogs 上所有仓库
+# 用法: ./gogs_backup_all.sh [备份目录]
+
+set -euo pipefail
+
+TOKEN="你的Access_Token"
+GOGS_URL="https://git.zhonjin.com:40717"
+BACKUP_DIR="${1:-./gogs_backup_$(date '+%Y%m%d')}"
+API="$GOGS_URL/api/v1"
+
+mkdir -p "$BACKUP_DIR"
+
+echo "📦 开始备份所有仓库到 $BACKUP_DIR"
+
+# 获取所有仓库
+repos=$(curl -sk -H "Authorization: token $TOKEN" "$API/user/repos" | \
+    python3 -c "import sys,json; [print(r['name']) for r in json.loads(sys.stdin.read())]")
+
+for repo in $repos; do
+    echo "  📥 正在备份: $repo"
+    curl -sk -H "Authorization: token $TOKEN" \
+        -o "$BACKUP_DIR/${repo}.zip" \
+        "$GOGS_URL/steven_roc/$repo/archive/master.zip"
+done
+
+echo ""
+echo "✅ 备份完成!共 $(echo "$repos" | wc -l) 个仓库"
+echo "📁 备份目录: $BACKUP_DIR"
+ls -lh "$BACKUP_DIR"
+```
+
+---
+
+## 六、SSH Key 配置
+
+```bash
+# 1. 生成密钥
+ssh-keygen -t ed25519 -C "steven_roc@zhonjin.com"
+
+# 2. 查看公钥
+cat ~/.ssh/id_ed25519.pub
+
+# 3. 添加到 Gogs
+# 登录 → 右上角头像 → 设置 → SSH 密钥 → 添加密钥
+
+# 4. 配置 SSH config(免端口参数)
+cat >> ~/.ssh/config << 'EOF'
+Host git.zhonjin.com
+    HostName git.zhonjin.com
+    Port 40717
+    User git
+    IdentityFile ~/.ssh/id_ed25519
+    StrictHostKeyChecking no
+EOF
+
+# 5. 测试连接
+ssh -T -p 40717 git@git.zhonjin.com
+```
+
+---
+
+## 七、常见问题排查
+
+| 问题 | 原因 | 解决方案 |
+|------|------|----------|
+| SSL 证书错误 | 自签名证书 | `GIT_SSL_NO_VERIFY=true` 或 `git config http.sslVerify false` |
+| 401 Unauthorized | Token 过期 | Gogs 设置 → 应用 → 重新生成 Token |
+| push 被拒绝 | 远程有更新 | 先 `git pull --rebase` 再 push |
+| 中文文件名乱码 | 编码问题 | `git config core.quotepath false` |
+| 大文件推送超时 | 文件太大 | 使用 `git lfs` 或分批推送 |
+| 忘记 Token | 安全原因无法找回 | 重新生成新 Token |
+
+---
+
+*本手册覆盖 Git Gogs 的完整操作:Web 界面、命令行、API、自动同步脚本、备份方案。*

+ 260 - 0
Git代码自动同步详细说明.md

@@ -0,0 +1,260 @@
+# Git 代码自动同步详细说明
+
+## 一、概述
+
+Git 代码自动同步是指通过自动化手段,将本地代码变更自动推送到远程仓库(或反之),减少手动操作步骤,保证多设备/多人协作时代码始终一致。
+
+---
+
+## 二、核心概念
+
+| 概念 | 说明 |
+|------|------|
+| **远程仓库(Remote)** | GitHub / GitLab / Gitee / GOG 等托管平台上的仓库 |
+| **本地仓库(Local)** | 开发者机器上的 `.git` 仓库 |
+| **Push** | 本地 → 远程,上传自己的提交 |
+| **Pull** | 远程 → 本地,拉取他人的更新 |
+| **Fetch + Merge** | Pull 的底层实现,先拉取再合并 |
+| **Hook** | Git 生命周期回调,可在特定事件时自动触发脚本 |
+
+---
+
+## 三、自动同步的几种典型方案
+
+### 方案 1:Git Hook 自动 Push(本地变更后自动推送)
+
+**适用场景**:本地提交后自动推送到远程,适合单人多设备同步。
+
+**实现步骤**:
+
+```bash
+# 1. 进入仓库目录
+cd /path/to/your/repo
+
+# 2. 创建 post-commit hook
+cat > .git/hooks/post-commit << 'EOF'
+#!/bin/bash
+# 提交后自动推送
+git push origin $(git branch --show-current) 2>&1 | tee -a .git/hooks/push.log
+EOF
+
+# 3. 赋予执行权限
+chmod +x .git/hooks/post-commit
+```
+
+**注意**:
+- 需要预先配置好 SSH Key 或 Credential,避免每次 push 都要求输入密码
+- 如果远程有冲突,push 会失败,需手动解决
+
+---
+
+### 方案 2:定时自动 Pull + Push(Cron / 任务计划)
+
+**适用场景**:定时双向同步,适合服务器和开发机之间保持同步。
+
+**Linux Cron 实现**:
+
+```bash
+# 编辑 crontab
+crontab -e
+
+# 每 5 分钟自动 pull + push
+*/5 * * * * cd /path/to/repo && git pull --rebase origin main && git push origin main >> /var/log/git-sync.log 2>&1
+```
+
+**Windows 任务计划实现**(PowerShell):
+
+```powershell
+# 创建同步脚本 sync.ps1
+Set-Location "C:\path\to\repo"
+git pull --rebase origin main
+git push origin main
+
+# 使用 Windows 任务计划程序创建定时任务
+$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\path\to\sync.ps1"
+$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5)
+Register-ScheduledTask -TaskName "GitAutoSync" -Action $action -Trigger $trigger
+```
+
+---
+
+### 方案 3:文件监听自动同步(inotify / fswatch)
+
+**适用场景**:文件保存后实时同步,无需等待 cron 周期。
+
+**Linux(inotifywait)**:
+
+```bash
+#!/bin/bash
+# auto-sync.sh
+REPO_DIR="/path/to/repo"
+BRANCH="main"
+
+inotifywait -m -r -e close_write,move,create,delete "$REPO_DIR" |
+while read -r directory events filename; do
+    cd "$REPO_DIR"
+    git add -A
+    git diff --cached --quiet || {
+        git commit -m "auto sync: $(date '+%Y-%m-%d %H:%M:%S')"
+        git push origin "$BRANCH"
+    }
+done
+```
+
+**macOS(fswatch)**:
+
+```bash
+brew install fswatch
+fswatch -r /path/to/repo --event Updated --event Created --event Removed |
+while read event; 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
+```
+
+---
+
+### 方案 4:使用 Git Bundle 离线同步
+
+**适用场景**:两台机器不联网,通过 U 盘等介质同步。
+
+```bash
+# 机器 A:打包变更
+git bundle create /media/usb/repo.bundle --all
+
+# 机器 B:从 bundle 拉取
+git pull /media/usb/repo.bundle main
+```
+
+---
+
+### 方案 5:使用 rsync + Git 混合方案
+
+**适用场景**:大量非代码文件(如编译产物)也需要一起同步。
+
+```bash
+#!/bin/bash
+# 1. 先 git 同步代码
+cd /path/to/repo
+git add -A && git commit -m "auto sync" && git push origin main
+
+# 2. 再 rsync 同步构建产物
+rsync -avz --exclude='.git' /path/to/repo/ user@remote:/path/to/repo/
+```
+
+---
+
+## 四、SSH Key 免密配置(前提条件)
+
+自动同步的前提是免密认证:
+
+```bash
+# 1. 生成 SSH Key(如果没有)
+ssh-keygen -t ed25519 -C "your_email@example.com"
+
+# 2. 复制公钥到远程平台
+cat ~/.ssh/id_ed25519.pub
+# 粘贴到 GitHub/GitLab/Gitee 的 SSH Keys 设置中
+
+# 3. 测试连接
+ssh -T git@github.com
+```
+
+---
+
+## 五、冲突处理策略
+
+自动同步最大的风险是冲突。推荐策略:
+
+| 场景 | 策略 |
+|------|------|
+| 单人多设备 | 使用 `pull --rebase`,避免无意义 merge commit |
+| 多人协作 | 不自动 push,改为 CI/CD 自动 pull 和测试 |
+| 纯文档类 | 可接受 occasional conflict,用 `ours` 或 `theirs` 策略 |
+| 二进制文件 | 使用 Git LFS 或改用 rsync 方案 |
+
+**自动解决冲突的配置**(仅限单人场景):
+
+```bash
+# 优先使用远程版本
+git config pull.rebase true
+git config rerere.enabled true
+
+# 或者冲突时自动选择一方
+git config merge.strategy.option theirs  # 远程优先
+```
+
+---
+
+## 六、完整自动化脚本示例
+
+```bash
+#!/bin/bash
+# git-auto-sync.sh - 完整的自动同步脚本
+# 用法: ./git-auto-sync.sh [仓库路径] [远程名] [分支名]
+
+REPO_DIR="${1:-.}"
+REMOTE="${2:-origin}"
+BRANCH="${3:-main}"
+LOG_FILE="$REPO_DIR/.git/auto-sync.log"
+
+log() {
+    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG_FILE"
+}
+
+cd "$REPO_DIR" || exit 1
+
+# 检查是否有未提交的变更
+if [[ -n "$(git status --porcelain)" ]]; then
+    git add -A
+    git commit -m "auto sync: $(date '+%Y-%m-%d %H:%M:%S')"
+    log "Committed local changes"
+fi
+
+# 拉取远程更新(rebase 方式)
+if git pull --rebase "$REMOTE" "$BRANCH" >> "$LOG_FILE" 2>&1; then
+    log "Pull success"
+else
+    log "Pull failed - conflict?"
+    git rebase --abort
+    exit 1
+fi
+
+# 推送本地提交
+if git push "$REMOTE" "$BRANCH" >> "$LOG_FILE" 2>&1; then
+    log "Push success"
+else
+    log "Push failed"
+    exit 1
+fi
+```
+
+---
+
+## 七、注意事项
+
+1. **安全性**:自动 push 前确保不会泄露敏感信息(密钥、密码等),建议使用 `.gitignore` 排除
+2. **网络依赖**:自动 push 失败时要有重试或告警机制
+3. **提交粒度**:自动 commit 会产生大量细粒度提交,建议定期 `git rebase -i` 整理
+4. **备份策略**:重要项目建议保留多个 remote 做冗余
+5. **权限隔离**:CI/CD 场景使用 Deploy Key 而非个人 SSH Key
+
+---
+
+## 八、推荐方案选择
+
+| 你的情况 | 推荐方案 |
+|----------|----------|
+| 单人,2台设备,偶尔同步 | 方案2:Cron 定时 Pull |
+| 单人,实时同步需求 | 方案3:文件监听自动同步 |
+| 多人协作 | 不建议自动 push,改为 CI/CD 自动测试 |
+| 离线环境 | 方案4:Git Bundle |
+| 大文件 + 代码混合 | 方案5:rsync + Git |
+
+---
+
+*文档生成时间:2026-10-02*

+ 37 - 0
README.md

@@ -0,0 +1,37 @@
+# Git Gogs 代码自动同步
+
+> 仓库地址: https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync  
+> 最后更新: 2026-10-08
+
+## 📦 仓库文件
+
+| 文件 | 说明 |
+|------|------|
+| `Git_Gogs完整操作手册.md` | 🔥 最全面:Web界面操作 + 命令行 + API + 自动同步脚本 + 备份方案 |
+| `Git代码自动同步详细说明.md` | 5种自动同步方案详解(Git Hook / Cron / inotify / Bundle / rsync) |
+| `Git_Gogs代码下载完整指南.md` | 从 Gogs 下载代码的完整方法 |
+
+## 🚀 快速操作
+
+```bash
+# 克隆
+git clone https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+
+# 推送
+git add -A && git commit -m "更新说明" && git push origin master
+
+# 拉取
+git pull --rebase origin master
+```
+
+## 🌐 Gogs Web 查看历史版本
+
+1. 打开 https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync
+2. 点击 **「提交数」** → 找到目标提交 → 点击 **「<>」** 按钮
+3. 即可浏览该版本的完整代码
+
+## 📋 仓库信息
+
+- **HTTPS 克隆**: https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync.git
+- **SSH 克隆**: git@git.zhonjin.com:steven_roc/git_gog_code_sync.git
+- **ZIP 下载**: https://git.zhonjin.com:40717/steven_roc/git_gog_code_sync/archive/master.zip