diff --git a/sync-repos.ps1 b/sync-repos.ps1 new file mode 100644 index 0000000..ae9390f --- /dev/null +++ b/sync-repos.ps1 @@ -0,0 +1,162 @@ +# Git Multi-Repository Sync Script (PowerShell) +# For syncing mirror repository to your personal repository + +param( + [switch]$UseRebase = $false, + [switch]$DryRun = $false +) + +function Write-ColorOutput { + param( + [string]$Message, + [string]$Color = "White" + ) + Write-Host $Message -ForegroundColor $Color +} + +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" + git status + return $false + } + return $true +} + +function Sync-Repositories { + Write-ColorOutput "Starting repository sync..." "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" + if (-not $DryRun) { + git fetch upstream + if ($LASTEXITCODE -ne 0) { + Write-ColorOutput "Failed to fetch upstream code!" "Red" + return + } + } else { + Write-ColorOutput "[DRY RUN] 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 $upstreamCommits "Gray" + + if ($UseRebase) { + Write-ColorOutput "Merging code using rebase..." "Green" + if (-not $DryRun) { + git rebase upstream/master + if ($LASTEXITCODE -ne 0) { + Write-ColorOutput "Rebase failed! Please resolve conflicts manually." "Red" + return + } + } else { + Write-ColorOutput "[DRY RUN] git rebase upstream/master" "Gray" + } + } else { + Write-ColorOutput "Merging code using merge..." "Green" + if (-not $DryRun) { + git merge upstream/master + if ($LASTEXITCODE -ne 0) { + Write-ColorOutput "Merge failed! Please resolve conflicts manually." "Red" + return + } + } else { + Write-ColorOutput "[DRY RUN] git merge upstream/master" "Gray" + } + } + + # Push to your repository + Write-ColorOutput "Pushing to your repository..." "Green" + if (-not $DryRun) { + if ($UseRebase) { + git push origin master --force-with-lease + } else { + git push origin master + } + if ($LASTEXITCODE -ne 0) { + Write-ColorOutput "Push failed!" "Red" + return + } + } else { + $pushCmd = if ($UseRebase) { "git push origin master --force-with-lease" } else { "git push origin master" } + Write-ColorOutput "[DRY RUN] $pushCmd" "Gray" + } + + Write-ColorOutput "Sync completed successfully!" "Green" + } else { + Write-ColorOutput "No new commits from upstream. Checking for local changes..." "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 $localCommits "Gray" + + Write-ColorOutput "Pushing local changes..." "Green" + if (-not $DryRun) { + git push origin master + if ($LASTEXITCODE -ne 0) { + Write-ColorOutput "Push failed!" "Red" + return + } + } else { + Write-ColorOutput "[DRY RUN] git push origin master" "Gray" + } + Write-ColorOutput "Push completed!" "Green" + } else { + Write-ColorOutput "Nothing to sync." "Blue" + } + } +} + +function Show-Help { + Write-ColorOutput "Git Multi-Repository Sync Script" "Cyan" + Write-ColorOutput "Usage: .\sync-repos.ps1 [options]" "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 "" + 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" +} + +# 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" + +# Show current configuration +Write-ColorOutput "Current remote repository configuration:" "Blue" +git remote -v + +Write-ColorOutput "" +if ($DryRun) { + Write-ColorOutput "Preview mode - no actual git commands will be executed" "Yellow" +} +if ($UseRebase) { + Write-ColorOutput "Using rebase mode" "Yellow" +} else { + Write-ColorOutput "Using merge mode" "Yellow" +} +Write-ColorOutput "" + +Sync-Repositories \ No newline at end of file diff --git a/sync_workflow.md b/sync_workflow.md new file mode 100644 index 0000000..15651e6 --- /dev/null +++ b/sync_workflow.md @@ -0,0 +1,128 @@ +# 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 ` +- 如果镜像仓库有大量更新,考虑创建一个新分支来处理合并