本文在Mac环境下编写。
有时需要在一个项目文件夹中管理多个远程仓库。例如,同一个项目需要提供给不同客户。这篇文章介绍如何在一个项目文件夹中管理多个远程仓库。
先创建一个空项目文件夹。
复制在GitHub上提前创建好的仓库的远程地址。

240606-154630
然后使用刚才复制的URL,添加第一个远程仓库。
git remote add origin https://github.com/user/first-repo.git
在GitHub上再创建一个仓库,并使用它的远程地址。
添加第二个远程时,建议使用不同于origin的名称。如果继续使用origin这个名称,操作可能会同时应用到两个远程仓库。
这里我将第二个远程命名为second-remote。
git remote add second-remote https://github.com/user/second-repo.git
在命令行进入项目文件夹,输入以下命令。
输出示例如下。
origin https://github.com/user/first-repo.git (fetch)
origin https://github.com/user/first-repo.git (push)
second-remote https://github.com/user/second-repo.git (fetch)
second-remote https://github.com/user/second-repo.git (push)
因为连接了两个远程,切换分支时需要指定远程名称。
git checkout second-remote/main
切换分支后,再继续进行开发。
git add .
git commit -m "commit message"
git push second-remote main
当然,也可以同时向两个远程分支推送。
git push origin main
git push second-remote main
拉取时,同样需要准确指定远程名称。
git pull origin main
git pull second-remote main
要移除一个远程仓库,请输入以下命令。
git remote remove origin
git remote remove second-remote
本文介绍了在一个项目文件夹中管理多个远程仓库的方法。使用这种方式,可以更方便地管理多个远程。