Git 多账户共存

I. 准备工作

1.1 去除 global 账户信息

1
2
git config --global --unset user.name
git config --global --unset user.email

添加通用配置

1
2
git config --global core.editor vim
git config --global color.ui true

1.2 添加多账户配置

1.2.1 SSH 多账户登录配置

📌 说明:
Linux:配置信息则保存在 ssh_config 文件中。
Windows:打开 git 命令行

1
2
3
4
cd                    // 到 git 家目录
cd .ssh/              // 到 ssh 默认隐藏目录
touch config          // 创建 ssh 配置文件
vim config            // 编辑 ssh 配置文件

编辑 ~/.ssh/config 配置文件如下(配置格式 参考 ):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# GitHub
Host github
	HostName github.com
	User git
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/github_ed25519
	ProxyCommand connect -S 127.0.0.1:10808 %h %p

# Gitee
Host gitee
	HostName gitee.com
	User git
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/gitee_ed25519

# Coding
Host coding
	HostName e.coding.net
	User git
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/coding_ed25519

1.2.2 配置用户名及邮箱

1
2
git config user.name "your_name"
git config user.email "your_email@example.com"

1.3 Git Config 优先级

📌 说明:
git config > git config --global > git config --system

1.4 Git 配置信息查看

1
git config --list

II. 使用 SSH 连接到 GitHub

2.1 SSH key 配置

Git 配置与使用

2.2 添加 SSH key 到 Gitee

Gitee SSH 公钥:点 这里

III. 分别推送不同仓库

在本地项目文件夹执行 git init 之后

3.1 添加远程仓库

a. 添加第一个远程仓库

1
git remote add github git@github.com:zanxj/Vimrc.git

或 HTTPS

1
git remote add github https://github.com/zanxj/Vimrc.git

b. 添加第二个远程仓库

1
git remote add gitee git@gitee.com:zanxj/vimrc.git

或 HTTPS

1
git remote add gitee https://gitee.com/zanxj/vimrc.git

c. 再添加第三个远程仓库

1
git remote add gitee git@e.coding.net:onlyou/Vim/vimrc.git

或 HTTPS

1
git remote add coding https://e.coding.net/onlyou/Vim/Vimrc.git

d. 打开 vim .git/config 配置如下:

1
2
3
4
5
6
[remote "github"]
	url = [https://github.com/zanxj/Vimrc.git](https://github.com/zanxj/Vimrc.git)
	fetch = +refs/heads/*:refs/remotes/github/*
[remote "gitee"]
	url = [https://gitee.com/zanxj/vimrc.git](https://gitee.com/zanxj/vimrc.git)
	fetch = +refs/heads/*:refs/remotes/gitee/*

3.2 查看关联的远程仓库

1
git remote -v

3.3 本地推送到远程仓库

1
2
git push github main
git push gitee main

IV. 同时推送多个账户

在本地项目文件夹执行 git init 之后

4.1 添加远程仓库

a. 添加第一个远程仓库

1
git remote add origin git@github.com:zanxj/Vimrc.git

或 HTTPS

1
git remote add origin https://github.com/zanxj/Vimrc.git

b. 添加第二个远程仓库

1
git remote set-url --add origin git@gitee.com:zanxj/vimrc.git

或 HTTPS

1
git remote set-url --add origin https://gitee.com/zanxj/vimrc.git

c. 再添加第三个远程仓库

1
git remote set-url --add origin git@e.coding.net:onlyou/Vim/vimrc.git

或 HTTPS

1
git remote set-url --add origin https://e.coding.net/onlyou/Vim/Vimrc.git

d. 打开 .gitconfig 配置如下:

1
2
3
4
[remote "origin"]
url = [https://gitee.com/jiaiqi/test.git](https://gitee.com/jiaiqi/test.git)
fetch = +refs/heads/*:refs/remotes/origin/*
url = [https://github.com/jiaiqi/test.git](https://github.com/jiaiqi/test.git)

4.2 查看关联的远程仓库

1
git remote -v

4.3 本地推送到远程仓库

1
git push origin --all