1. 准备工作

1.1 去除 global 账户信息

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

添加通用配置

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

1.2 添加多账户配置

1.2.1 SSH 多账户登录配置

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

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

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

 1# 关于别名
 2# Host 是别名,HostName 是真正的域名。
 3# 得益于别名,你可以直接以别名访问地址。例如:
 4# 无别名: git clone git@github.com:torvalds/linux.git
 5# 有别名: git clone github:torvalds/linux.git
 6# 本例中使用与域名一致的别名,以免错误的配置导致登录不上。
 7
 8# 关于代理
 9# SOCKS 代理格式: ProxyCommand connect -S localhost:1080  %h %p
10# HTTP 代理格式: ProxyCommand connect -H localhost:1080  %h %p
11## SSH 代理依赖外部程序,这里使用了 Git for Windows 同捆的 connect.exe。
12## Linux 下使用该代理方式需要额外安装 connect-proxy。
13
14# GitHub
15Host github
16	HostName github.com
17	User git
18	PreferredAuthentications publickey
19	IdentityFile ~/.ssh/github_ed25519
20	ProxyCommand connect -S 127.0.0.1:10808 %h %p
21
22# Gitee
23Host gitee
24	HostName gitee.com
25	User git
26	PreferredAuthentications publickey
27	IdentityFile ~/.ssh/gitee_ed25519
28
29# Coding
30Host coding
31	HostName e.coding.net
32	User git
33	PreferredAuthentications publickey
34	IdentityFile ~/.ssh/coding_ed25519

1.2.2 配置用户名及邮箱

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

1.3 Git Config 优先级

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

1.4 Git 配置信息查看

1git config --list

2. 使用 SSH 连接到 GitHub

2.1 SSH key 配置

参考 👉 Git 教程

2.2 添加 SSH key 到 Gitee

Gitee SSH 公钥:点这里

3. 克隆远程仓库

3.1 有别名克隆

1git clone github:torvalds/linux.git

3.2 无别名克隆

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

4. 本地关联远程仓库

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

4.1 添加远程仓库

a. 添加第一个远程仓库

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

或 HTTPS

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

b. 添加第二个远程仓库

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

或 HTTPS

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

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

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

或 HTTPS

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

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

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

4.2 查看关联的远程仓库

1git remote -v

4.3 本地推送到远程仓库

1git push github main
2git push gitee main

5. 同时推送多个账户

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

5.1 添加远程仓库

a. 添加第一个远程仓库

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

或 HTTPS

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

b. 添加第二个远程仓库

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

或 HTTPS

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

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

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

或 HTTPS

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

d. 打开 .gitconfig 配置如下:

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

5.2 查看关联的远程仓库

1git remote -v

5.3 本地推送到远程仓库

1git push origin --all