一份代码如何多个git仓库管理

  1. 查看当前代码仓库情况
user@user
$ git remote -v
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

fetch 代表拉取路径
push 代表推送路径

  1. 执行追加第二个仓库
# 添加第一个远程仓库(通常默认) 这一步必须
git remote set-url origin https://github.com/user/repo.git

# 添加第二个远程仓库(--add 参数追加,而不是覆盖)
git remote set-url --add --push origin https://github.com/user2/repo.git

** 注意:第一步 set-url --push(不带 --add)会重置 push 列表,只保留这个 URL;第二步 --add --push 才是追加。 **

  1. 再查看远程分支
user@user
$ git remote -v
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)
origin  https://github.com/user2/repo.git (push)

** 这样拉取只会从 https://github.com/user/repo.git;推送则推两个 **

  1. 开始将代码推送到远程分支
git push origin master

这时可能有各种问题:

  • 这时可能第二个仓库推送不了,确保仓库二代码被覆盖没问题情况下,进行一次强推
git push origin master -f
  • 这是有可能提示仓库二受保护,不允许强推

手动或命令关闭仓库二的保护状态

再执行一次强推就OK了。

  1. 拉取代码(选择性拉取)
git pull #默认拉取`https://github.com/user/repo.git`
git pull https://github.com/user2/repo.git master #选择性拉取仓库二的master分支
posted @ 2026-01-07 09:50  Nine4酷  阅读(7)  评论(0)    收藏  举报