瀏覽代碼

docs: 完整推送 - README + 自动同步方案 + 下载指南

steven_roc 2 天之前
當前提交
d07f608f21
共有 3 個文件被更改,包括 610 次插入 和 0 次删除
  1. 276 0
      Git_Gogs代码下载完整指南.md
  2. 260 0
      Git代码自动同步详细说明.md
  3. 74 0
      README.md

+ 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
+```
+
+---
+
+*文档维护: 每次下载方式变更时更新本文档*

+ 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*

+ 74 - 0
README.md

@@ -0,0 +1,74 @@
+# Git Gogs 代码自动同步
+
+> 多种方案实现本地代码与 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` — 本文件,项目概览
+
+## 快速开始
+
+### 方案 1:Git Hook(推荐单人使用)
+
+```bash
+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
+```
+
+### 方案 2:Cron 定时同步
+
+```bash
+# 每 5 分钟自动 pull + push
+*/5 * * * * cd /path/to/repo && git pull --rebase origin main && git push origin main >> /var/log/git-sync.log 2>&1
+```
+
+### 方案 3:文件监听实时同步
+
+```bash
+# 需要 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
+```
+
+## 前提条件
+
+1. **SSH Key 免密配置**:确保 `ssh -T git@git.zhonjin.com` 能通过认证
+2. **Git 基本配置**:`git config user.name` 和 `user.email` 已设置
+3. **远程仓库已创建**:在 Gogs 上新建空仓库(不勾选初始化)
+
+## 安全提示
+
+- Access Token 定期轮换,避免泄露
+- 敏感文件(密钥、密码)加入 `.gitignore`
+- 自动 push 前确认无敏感信息提交
+
+---
+
+*Gogs 服务器: https://git.zhonjin.com:40717*