


























需要一个正常的可工作仓库,而不是裸镜像仓库。以下是在不使用 --mirror 选项的情况下,拉取所有分支和标签并推送到新仓库的步骤:
首先,正常克隆源仓库:
# 克隆源仓库(默认只会检出 main 或 master 分支)
git clone <源仓库URL> repo-copy
cd repo-copy
默认情况下,git clone 只会创建和检出默认分支。你需要额外步骤来获取和创建所有远程分支的本地跟踪:
# 获取所有远程分支的信息
git fetch --all
# 为每个远程分支创建本地跟踪分支
git branch -r | grep -v '\->' | while read remote; do
git branch --track "${remote#origin/}" "$remote"
done
# 拉取所有分支的最新更改
git fetch --all
git pull --all
确保所有标签也被获取:
# 拉取所有标签
git fetch --tags
# 添加目标远程仓库
git remote add target <目标仓库URL>
# 推送所有分支到目标仓库
git push target --all
# 推送所有标签到目标仓库
git push target --tags
下面是一个完整的脚本,可以一键完成整个过程:
# 克隆源仓库
git clone <源仓库URL> repo-copy
cd repo-copy
# 获取所有远程分支信息
git fetch --all
# 为远程分支创建本地跟踪分支
git branch -r | grep -v '\->' | while read remote; do
git branch --track "${remote#origin/}" "$remote"
done
# 拉取所有分支和标签的最新更改
git fetch --all
git pull --all
git fetch --tags
# 添加目标远程仓库
git remote add target <目标仓库URL>
# 推送所有内容到目标仓库
git push target --all
git push target --tags
echo "所有分支和标签已成功推送到目标仓库"
在较新版本的 Git 中,你可以使用以下简化命令:
# 克隆仓库
git clone <源仓库URL> repo-copy
cd repo-copy
# 获取所有分支并设置本地跟踪
git fetch origin
git checkout -b local_branch origin/remote_branch # 对需要的每个远程分支重复此命令
# 获取所有标签
git fetch --tags
# 添加新远程仓库并推送
git remote add target <目标仓库URL>
git push target --all
git push target --tags
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。