You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自动更新本地文件夹中所有GitHub仓库至远端版本?

批量更新本地GitHub仓库的方法

针对你本地all_repos目录下的多个GitHub仓库,可以通过脚本批量完成更新操作,以下是不同系统的实现方式:

Linux/macOS(Shell脚本)

进入all_repos目录后,执行以下脚本遍历并更新所有git仓库:

cd /path/to/all_repos
for dir in */; do
    # 检查当前子目录是否为git仓库
    if [ -d "$dir/.git" ]; then
        echo "正在更新仓库: $dir"
        cd "$dir"
        # 拉取当前分支的最新代码
        git pull origin $(git rev-parse --abbrev-ref HEAD)
        cd ..
    fi
done

说明

  • 脚本会自动识别子目录中的git仓库(通过.git文件夹判断)
  • git rev-parse --abbrev-ref HEAD会自动获取当前仓库的活跃分支,无需手动指定
  • 如果仓库存在未提交的本地修改,git pull会失败,可根据需求在git pull前添加git stash暂存修改,拉取后用git stash pop恢复(注意可能出现冲突)

Windows(PowerShell脚本)

打开PowerShell,进入all_repos目录后执行:

Set-Location "C:\path\to\all_repos"
Get-ChildItem -Directory | ForEach-Object {
    $gitDir = Join-Path -Path $_.FullName -ChildPath ".git"
    if (Test-Path -Path $gitDir) {
        Write-Host "正在更新仓库: $($_.Name)"
        Set-Location $_.FullName
        git pull origin $(git rev-parse --abbrev-ref HEAD)
        Set-Location ..
    }
}

说明

  • 逻辑与Shell脚本一致,适配Windows环境的路径和命令格式
  • 同样会自动检测git仓库并拉取当前分支最新代码

额外注意事项

  • 确保本地仓库已正确关联GitHub远程仓库(默认远程名为origin,若不同需替换脚本中的origin)
  • 若需要固定拉取某个分支(如main),可将$(git rev-parse --abbrev-ref HEAD)替换为具体分支名,比如git pull origin main

内容的提问来源于stack exchange,提问作者Caridorc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 10:15:15