diff --git a/sync-repos.ps1 b/sync-repos.ps1 index 7e97937..4946215 100644 --- a/sync-repos.ps1 +++ b/sync-repos.ps1 @@ -1,4 +1,5 @@ -# Git Multi-Repository Sync Script (PowerShell) +# Git多仓库同步脚本 (PowerShell) +# 用于同步镜像仓库到你的个人仓库 param( [switch]$UseRebase = $false, @@ -16,7 +17,7 @@ function Write-ColorOutput { function Check-GitStatus { $status = git status --porcelain if ($status) { - Write-ColorOutput "Warning: Working directory is not clean. Please commit or stash your changes first!" "Yellow" + Write-ColorOutput "警告: 工作目录不干净,请先提交或储藏你的更改!" "Yellow" git status return $false } @@ -24,124 +25,129 @@ function Check-GitStatus { } function Sync-Repositories { - Write-ColorOutput "Starting repository sync..." "Cyan" + Write-ColorOutput "开始同步镜像仓库..." "Cyan" - # Check working directory status + # 检查工作目录状态 if (-not (Check-GitStatus)) { if (-not $DryRun) { return } } - # Fetch latest code from upstream - Write-ColorOutput "Fetching latest code from upstream..." "Green" + # 获取镜像仓库最新代码 + Write-ColorOutput "正在获取upstream最新代码..." "Green" if (-not $DryRun) { git fetch upstream if ($LASTEXITCODE -ne 0) { - Write-ColorOutput "Failed to fetch upstream code!" "Red" + Write-ColorOutput "获取upstream代码失败!" "Red" return } } else { - Write-ColorOutput "[DRY RUN] git fetch upstream" "Gray" + Write-ColorOutput "[预览模式] git fetch upstream" "Gray" } - # Check if there are new commits + # 检查是否有新的提交 $upstreamCommits = git log HEAD..upstream/master --oneline if ($upstreamCommits) { - Write-ColorOutput "Found new commits from upstream:" "Yellow" + Write-ColorOutput "发现镜像仓库有新的提交:" "Yellow" Write-ColorOutput $upstreamCommits "Gray" if ($UseRebase) { - Write-ColorOutput "Merging code using rebase..." "Green" + Write-ColorOutput "正在使用rebase合并代码..." "Green" if (-not $DryRun) { git rebase upstream/master if ($LASTEXITCODE -ne 0) { - Write-ColorOutput "Rebase failed! Please resolve conflicts manually." "Red" - Write-ColorOutput "After resolving conflicts, run: git rebase --continue" "Yellow" + Write-ColorOutput "Rebase失败!请手动解决冲突后再继续。" "Red" + Write-ColorOutput "解决冲突后请运行: git rebase --continue" "Yellow" return } } else { - Write-ColorOutput "[DRY RUN] git rebase upstream/master" "Gray" + Write-ColorOutput "[预览模式] git rebase upstream/master" "Gray" } } else { - Write-ColorOutput "Merging code using merge..." "Green" + Write-ColorOutput "正在使用merge合并代码..." "Green" if (-not $DryRun) { git merge upstream/master if ($LASTEXITCODE -ne 0) { - Write-ColorOutput "Merge failed! Please resolve conflicts manually." "Red" - Write-ColorOutput "After resolving conflicts, run: git commit" "Yellow" + Write-ColorOutput "Merge失败!请手动解决冲突后再继续。" "Red" + Write-ColorOutput "解决冲突后请运行: git commit" "Yellow" return } } else { - Write-ColorOutput "[DRY RUN] git merge upstream/master" "Gray" + Write-ColorOutput "[预览模式] git merge upstream/master" "Gray" } } - Write-ColorOutput "Sync completed successfully!" "Green" + Write-ColorOutput "同步完成!" "Green" Write-ColorOutput "" - Write-ColorOutput "Next steps:" "Cyan" - Write-ColorOutput " Please manually push to your repository: git push origin master" "White" + Write-ColorOutput "下一步操作提示:" "Cyan" + Write-ColorOutput " 请手动推送到你的仓库: git push origin master" "White" if ($UseRebase) { - Write-ColorOutput " (If using rebase, you might need: git push origin master --force-with-lease)" "Yellow" + Write-ColorOutput " (如果使用了rebase,可能需要: git push origin master --force-with-lease)" "Yellow" } } else { - Write-ColorOutput "No new commits from upstream. Checking for local changes..." "Blue" + Write-ColorOutput "镜像仓库没有新的提交,检查本地是否有未推送的修改..." "Blue" - # Check if there are local changes to push + # 检查是否有本地修改需要推送 $localCommits = git log origin/master..HEAD --oneline if ($localCommits) { - Write-ColorOutput "Found local changes to push:" "Yellow" + Write-ColorOutput "发现本地有未推送的修改:" "Yellow" Write-ColorOutput $localCommits "Gray" Write-ColorOutput "" - Write-ColorOutput "Next steps:" "Cyan" - Write-ColorOutput " Please manually push to your repository: git push origin master" "White" + Write-ColorOutput "下一步操作提示:" "Cyan" + Write-ColorOutput " 请手动推送到你的仓库: git push origin master" "White" } else { - Write-ColorOutput "Nothing to sync." "Blue" + Write-ColorOutput "没有需要同步的内容。" "Blue" } } } function Show-Help { - Write-ColorOutput "Git Multi-Repository Sync Script" "Cyan" - Write-ColorOutput "Usage: .\sync-repos.ps1 [options]" "White" + Write-ColorOutput "Git多仓库同步脚本" "Cyan" + Write-ColorOutput "用法: .\sync-repos.ps1 [选项]" "White" Write-ColorOutput "" - Write-ColorOutput "Options:" "Yellow" - Write-ColorOutput " -UseRebase Use rebase instead of merge" "White" - Write-ColorOutput " -DryRun Preview mode, don't execute actual git commands" "White" - Write-ColorOutput " -Help Show this help message" "White" + Write-ColorOutput "选项:" "Yellow" + Write-ColorOutput " -UseRebase 使用rebase代替merge进行合并" "White" + Write-ColorOutput " -DryRun 预览模式,不执行实际的git命令" "White" + Write-ColorOutput " -Help 显示此帮助信息" "White" Write-ColorOutput "" - Write-ColorOutput "Examples:" "Yellow" - Write-ColorOutput " .\sync-repos.ps1 # Sync using merge" "White" - Write-ColorOutput " .\sync-repos.ps1 -UseRebase # Sync using rebase" "White" - Write-ColorOutput " .\sync-repos.ps1 -DryRun # Preview mode" "White" + Write-ColorOutput "示例:" "Yellow" + Write-ColorOutput " .\sync-repos.ps1 # 使用merge同步" "White" + Write-ColorOutput " .\sync-repos.ps1 -UseRebase # 使用rebase同步" "White" + Write-ColorOutput " .\sync-repos.ps1 -DryRun # 预览模式" "White" Write-ColorOutput "" - Write-ColorOutput "Note: Script will NOT automatically push to your repository." "Yellow" + Write-ColorOutput "注意: 脚本不会自动推送到你的仓库,需要手动执行推送命令。" "Yellow" } -# Main program +# 主程序 if ($args -contains "-Help" -or $args -contains "--help" -or $args -contains "-h") { Show-Help return } -Write-ColorOutput "Git Multi-Repository Sync Tool" "Cyan" -Write-ColorOutput "==============================" "Cyan" +Write-ColorOutput "Git多仓库同步工具" "Cyan" +Write-ColorOutput "===================" "Cyan" -# Show current configuration -Write-ColorOutput "Current remote repository configuration:" "Blue" +# 显示当前配置 +Write-ColorOutput "当前远程仓库配置:" "Blue" git remote -v Write-ColorOutput "" if ($DryRun) { - Write-ColorOutput "Preview mode - no actual git commands will be executed" "Yellow" + Write-ColorOutput "预览模式 - 不会执行实际的git命令" "Yellow" } if ($UseRebase) { - Write-ColorOutput "Using rebase mode" "Yellow" + Write-ColorOutput "使用rebase模式" "Yellow" } else { - Write-ColorOutput "Using merge mode" "Yellow" + Write-ColorOutput "使用merge模式" "Yellow" } Write-ColorOutput "" -Sync-Repositories \ No newline at end of file +Sync-Repositories + +# 等待用户按键后再关闭 +Write-ColorOutput "" +Write-ColorOutput "按任意键继续..." "Green" +$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") \ No newline at end of file diff --git a/sync_workflow.md b/sync_workflow.md deleted file mode 100644 index 15651e6..0000000 --- a/sync_workflow.md +++ /dev/null @@ -1,128 +0,0 @@ -# Git多远程仓库同步工作流 - -## 仓库配置 - -- **upstream**: `http://192.168.189.2:8418/amr.core/web-amr` (镜像仓库,只读) -- **origin**: `http://192.168.189.2:8418/xudan/web-map` (你的仓库,可读写) - -## 日常工作流程 - -### 1. 从镜像仓库获取最新代码 - -```bash -# 获取镜像仓库的最新代码 -git fetch upstream - -# 查看镜像仓库的更新 -git log HEAD..upstream/master --oneline -``` - -### 2. 合并镜像仓库的最新代码到本地 - -```bash -# 方法1: 使用merge (推荐,保留完整历史) -git merge upstream/master - -# 方法2: 使用rebase (如果你想要线性历史) -git rebase upstream/master -``` - -### 3. 推送到你的仓库 - -```bash -# 推送到你的仓库 -git push origin master -``` - -## 完整的同步脚本 - -### 方法1: 使用merge - -```bash -#!/bin/bash -echo "开始同步镜像仓库..." - -# 获取镜像仓库最新代码 -git fetch upstream - -# 检查是否有新的提交 -if git log HEAD..upstream/master --oneline | grep -q .; then - echo "发现镜像仓库有新的提交,开始合并..." - - # 合并镜像仓库的代码 - git merge upstream/master - - # 推送到你的仓库 - git push origin master - - echo "同步完成!" -else - echo "镜像仓库没有新的提交,检查本地是否有未推送的修改..." - - # 检查是否有本地修改需要推送 - if git log origin/master..HEAD --oneline | grep -q .; then - echo "发现本地有未推送的修改,推送中..." - git push origin master - echo "推送完成!" - else - echo "没有需要同步的内容。" - fi -fi -``` - -### 方法2: 使用rebase (如果你想要更干净的历史) - -```bash -#!/bin/bash -echo "开始同步镜像仓库..." - -# 获取镜像仓库最新代码 -git fetch upstream - -# 检查是否有新的提交 -if git log HEAD..upstream/master --oneline | grep -q .; then - echo "发现镜像仓库有新的提交,开始rebase..." - - # rebase到镜像仓库的最新代码 - git rebase upstream/master - - # 推送到你的仓库 (可能需要force push) - git push origin master --force-with-lease - - echo "同步完成!" -else - echo "镜像仓库没有新的提交,检查本地是否有未推送的修改..." - - # 检查是否有本地修改需要推送 - if git log origin/master..HEAD --oneline | grep -q .; then - echo "发现本地有未推送的修改,推送中..." - git push origin master - echo "推送完成!" - else - echo "没有需要同步的内容。" - fi -fi -``` - -## 推荐的工作流程 - -1. **每天开始工作前**: 运行同步脚本,确保有最新的镜像仓库代码 -2. **进行本地开发**: 正常开发你的功能 -3. **提交本地修改**: `git add` 和 `git commit` -4. **推送前再次同步**: 运行同步脚本,确保没有冲突 -5. **推送到你的仓库**: `git push origin master` - -## 处理冲突 - -如果在合并时遇到冲突: - -1. 解决冲突文件 -2. `git add` 冲突文件 -3. `git commit` 完成合并 -4. `git push origin master` - -## 注意事项 - -- 使用 `--force-with-lease` 而不是 `--force` 来避免意外覆盖其他人的提交 -- 定期清理本地分支: `git branch -d ` -- 如果镜像仓库有大量更新,考虑创建一个新分支来处理合并