方法1:每次push、pull时分开操作
- 首先,查看本地仓库所关联的远程仓库:(假定最初仅关联了一个远程仓库)
$ git remote -v
origin https://gitee.com/mayuxia/question-uniapp.git (fetch)
origin https://gitee.com/mayuxia/question-uniapp.git (push)
2.然后,用 git remote add 添加一个远程仓库,其中name可以任意指定(对应上面的origin部分)
$ git remote add company https://gitee.com/neteasy_1/student-aid-app.git
再次查看本地仓库所关联的远程仓库,可以发现成功关联了两个远程仓库:
$ git remote -v
company https://gitee.com/neteasy_1/student-aid-app.git (fetch)
company https://gitee.com/neteasy_1/student-aid-app.git (push)
origin https://gitee.com/mayuxia/question-uniapp.git (fetch)
origin https://gitee.com/mayuxia/question-uniapp.git (push)
此后,若需进行push操作,则需要指定目标仓库,git push ,对这两个远程仓库分别操作:
$ git push origin master
$ git push company master
同理,pull操作也需要指定从哪个远程仓库拉取,git pull ,从这两个仓库中选择其一:
$ git pull origin master
$ git pull company master
注意在关联仓库后无法push可以强制推送(通常情况不建议)
如果你确定两个分支的内容是安全的,并且没有冲突,可以使用 --allow-unrelated-histories 选项强制合并:
$ git pull origin <branch-name> --allow-unrelated-histories
或者:
$ git merge <branch-name> --allow-unrelated-histories
确保两个历史都是安全的。如下:
$ git pull origin master --allow-unrelated-histories
注意:这种方法会直接合并两个分支的内容,可能会导致意外的冲突或覆盖。使用时需谨慎。
方法2: push和pull无需额外操作
- 先查看已有的远程仓库:(假定最初仅关联了一个远程仓库)
$ git remote -v
origin https://gitee.com/mayuxia/question-uniapp.git (fetch)
origin https://gitee.com/mayuxia/question-uniapp.git (push)
2.不额外添加远程仓库,而是给现有的远程仓库添加额外的 URL。使用 git remote set-url -add ,给已有的名为name的远程仓库添加一个远程地址,比如:
$ git remote set-url --add origin https://gitee.com/neteasy_1/student-aid-app.git
再次查看所关联的远程仓库:
$ git remote -v
origin https://gitee.com/mayuxia/question-uniapp.git (fetch)
origin https://gitee.com/mayuxia/question-uniapp.git (push)
origin https://gitee.com/neteasy_1/student-aid-app.git (push)
可以看到,我们并没有如 方法1 一般增加远程仓库的数目,而是给一个远程仓库赋予了多个地址(或者准确地说,多个用于push的地址)。
因此,这样设置后的push 和pull操作与最初的操作完全一致,不需要分开操作。