250722-Git代码库迁移脚本命令(git 仓库迁移 git clone --mirror git push --mirror)

使用 git clone –mirror 可以整体搬移或复制一个 Git 仓库,包括所有的分支、标签和远程配置。以下是具体步骤:

操作脚本命令

1
2
3
4
git clone --mirror https://github.com/user/source-repo.git
cd source-repo.git
git remote set-url --push origin https://github.com/user/new-repo.git
git push --mirror

1,克隆源仓库

使用 git clone –mirror 命令来克隆源仓库。这个命令会创建一个裸仓库,包含所有的 Git 数据。
git clone –mirror
例如:

1
git clone --mirror https://github.com/user/source-repo.git

2,进入克隆的仓库目录

克隆完成后,进入到新创建的仓库目录。这个目录通常以 .git 结尾。

1
cd source-repo.git

3,设置新的远程仓库

使用 git remote set-url 命令来设置新的远程仓库 URL。
git remote set-url –push origin

例如:

1
git remote set-url --push origin https://github.com/user/new-repo.git

4,推送到新的远程仓库

使用 git push –mirror 命令将所有的分支和标签推送到新的远程仓库。

1
git push --mirror

通过这些步骤,我们可以将一个 Git 仓库完整地复制到另一个远程位置。请确保在执行这些操作之前,你有相应的权限访问和推送到目标仓库。