1. 准备工作

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
22
23
24
25
26
27
28
29
30
31
32
33
34
# 关于别名
# Host 是别名,HostName 是真正的域名。
# 得益于别名,你可以直接以别名访问地址。例如:
# 无别名: git clone git@github.com:torvalds/linux.git
# 有别名: git clone github:torvalds/linux.git
# 本例中使用与域名一致的别名,以免错误的配置导致登录不上。

# 关于代理
# SOCKS 代理格式: ProxyCommand connect -S localhost:1080  %h %p
# HTTP 代理格式: ProxyCommand connect -H localhost:1080  %h %p
## SSH 代理依赖外部程序,这里使用了 Git for Windows 同捆的 connect.exe。
## Linux 下使用该代理方式需要额外安装 connect-proxy。

# 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

2. 使用 SSH 连接到 GitHub

2.1 SSH key 配置

参考 👉 Git 教程

2.2 添加 SSH key 到 Gitee

Gitee SSH 公钥:点这里

3. 克隆远程仓库

3.1 有别名克隆

1
git clone github:torvalds/linux.git

3.2 无别名克隆

1
git clone git@github.com:torvalds/linux.git

4. 本地关联远程仓库

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

4.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/*

4.2 查看关联的远程仓库

1
git remote -v

4.3 本地推送到远程仓库

1
2
git push github main
git push gitee main

5. 同时推送多个账户

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

5.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)

5.2 查看关联的远程仓库

1
git remote -v

5.3 本地推送到远程仓库

1
git push origin --all