得之我幸 失之我命

when someone abandons you,it is him that gets loss because he lost someone who truly loves him but you just lost one who doesn’t love you.

git 本地仓库同时推到多个远程仓库

以前用 coding,后来它被企鹅收购了,也变了,虽然也不知道它是因为企鹅爸爸变了还是因为变了才受到企鹅爸爸的亲睐,总之是不好用了

可能是它变差了,所以感觉 github 变好用了,就想着把一部分常用的仓库转移到 github 上去,但是又不想把鸡蛋放在同一个篮子里

但是查了资料以后才发现,git 把一个本地仓库 push 到多个远程仓库也是可以分两种情况的:

  1. 只有一个库可以 pull,其他库都只能 push,并且是一条命令里完成 push
  2. 所有的库都可以 pull/push,但并不那么同时,至少一条 push 命令做不到

本地已经有测试代码库 test,以同时 push 到 coding 的 test_remote 和 test_remote2 为例,展示这两种情况

1
2
3
4
5
6
7
$ pwd
/Users/test/test
$ ls
README.md
$ git branch -vv
* main 259223b first commit
$ git remote -vv # 空,没有关联任何远程仓库

只有一个库可以 pull,其他库都只能 push,并且是一条命令里完成 push

  1. 添加远程仓库
1
2
3
4
5
6
# 通过「git remote set-url --add 别名 仓库地址」添加新的远程仓库
$ git remote set-url --add origin git@e.coding.net:test/test_git/test_remote.git
error: No such remote 'origin' # 因为本地仓库并没有关联任何远程仓库
# 第一个关联的远程仓库,应该通过 「git remote add 别名 仓库地址」添加新的远程仓库
$ git remote add origin git@e.coding.net:test/test_git/test_remote.git
$ git remote set-url --add origin git@e.coding.net:test/test_git/test_remote2.git
  1. 查看本地仓库关联的远程仓库情况
1
2
3
4
5
# 本地仓库可以同时推送到 test_remote 和 test_remote2,但只能从 test_remote 拉取
$ git remote -vv
origin git@e.coding.net:test/test_git/test_remote.git (fetch)
origin git@e.coding.net:test/test_git/test_remote.git (push)
origin git@e.coding.net:test/test_git/test_remote2.git (push)
  1. push 到远程仓库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -u 是 --set-upstream 的简写,作用是将本地分支与远程分支关联(设置为上游分支)
# 首次推送分支时推荐加 -u,以便自动关联远程分支
# 后续推送如果已经设置过上游分支,就不需要加 -u,直接用 git push
$ git push -u origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 246 bytes | 246.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To e.coding.net:test/test_git/test_remote.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 246 bytes | 246.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To e.coding.net:test/test_git/test_remote2.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.

插一嘴

  1. 步骤 1 的添加远程仓库可以通过修改 git 的 config 文件来实现
1
2
3
4
5
6
# 找到remote origin,添加url = 仓库地址
$ git config -e # 或者直接编辑 vim .git/config
[remote "origin"]
url = git@e.coding.net:test/test_git/test_remote.git
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@e.coding.net:test/test_git/test_remote2.git
  1. 那如果想切换成只能从 test_remote2 拉取呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 直接修改第一个 url 的内容
# 即,并不是 fetch 在谁之后就允许从谁 pull,而是看第一个 url 是谁就只能从谁 pull
$ git config -e
[remote "origin"]
url = git@e.coding.net:test/test_git/test_remote2.git
url = git@e.coding.net:test/test_git/test_remote.git
fetch = +refs/heads/*:refs/remotes/origin/*

# 用 git remote set-url
# 但是并不能直接设置,而是需要让 origin 仅保留一个 url
# 即,需要先删除多余的
$ git remote set-url origin git@e.coding.net:test/test_git/test_remote2.git
warning: remote.origin.url has multiple values
fatal: could not set 'remote.origin.url' to 'git@e.coding.net:test/test_git/test_remote2.git'

多个库可以同时 pull/push,至少一条 push 命令做不到

  1. 添加远程仓库,并为每个远程仓库配置一个唯一的名称
1
2
$ git remote add test_remote git@e.coding.net:test/test_git/test_remote.git
$ git remote add test_remote2 git@e.coding.net:test/test_git/test_remote2.git
  1. 查看本地仓库关联的远程仓库情况
1
2
3
4
5
$ git remote -vv
test_remote git@e.coding.net:test/test_git/test_remote.git (fetch)
test_remote git@e.coding.net:test/test_git/test_remote.git (push)
test_remote2 git@e.coding.net:test/test_git/test_remote2.git (fetch)
test_remote2 git@e.coding.net:test/test_git/test_remote2.git (push)
  1. push 到远程仓库
1
2
$ git push test_remote main
$ git push test_remote2 main

be slow to promise and quick to perform.