git如何添加多个远程仓库?

5 min read

可以使用git remote add命令添加多个远程仓库,具体步骤如下:

  1. 在命令行终端进入项目根目录

  2. 输入以下命令添加第一个远程仓库

git remote add <远程仓库名称1> <远程仓库地址1>

例如:

git remote add origin https://github.com/user/repo.git
  1. 输入以下命令添加第二个远程仓库
git remote add <远程仓库名称2> <远程仓库地址2>

例如:

git remote add backup https://gitlab.com/user/repo.git
  1. 使用git remote -v命令查看当前所有远程仓库
git remote -v

即可看到类似如下的结果:

origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)
backup  https://gitlab.com/user/repo.git (fetch)
backup  https://gitlab.com/user/repo.git (push)

这样就添加了两个远程仓库,分别用originbackup来表示。以后就可以使用这两个名称分别向不同的远程仓库推送代码了。