I. 准备工作
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# GitHub
2Host github
3 HostName github.com
4 User git
5 PreferredAuthentications publickey
6 IdentityFile ~/.ssh/github_ed25519
7 ProxyCommand connect -S 127.0.0.1:10808 %h %p
8
9# Gitee
10Host gitee
11 HostName gitee.com
12 User git
13 PreferredAuthentications publickey
14 IdentityFile ~/.ssh/gitee_ed25519
15
16# Coding
17Host coding
18 HostName e.coding.net
19 User git
20 PreferredAuthentications publickey
21 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
II. 使用 SSH 连接到 GitHub
2.1 SSH key 配置
参考 👉 Git 教程
2.2 添加 SSH key 到 Gitee
Gitee SSH 公钥:点这里
III. 分别推送不同仓库
在本地项目文件夹执行 git init
之后
3.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/*
3.2 查看关联的远程仓库
1git remote -v
3.3 本地推送到远程仓库
1git push github main
2git push gitee main
IV. 同时推送多个账户
在本地项目文件夹执行 git init 之后
4.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)
4.2 查看关联的远程仓库
1git remote -v
4.3 本地推送到远程仓库
1git push origin --all