https://juejin.im/post/6844904180633600007
https://www.jianshu.com/p/68578d52470c
Git共有三個級別的config文件,分別是:
- system :%GitPath%\mingw64\etc\gitconfig文件
- global:$home.gitconfig文件
- local:%RepoPath%.git\config文件
其中%GitPath%
為Git的安裝路徑,%RepoPath%
為某倉庫的本地路徑。
1. 刪掉全局配置
git config --global --list
$ git config --global --unset user.name "你的名字"
$ git config --global --unset user.email "你的郵箱"
2. 為不同賬戶配置ssh秘鑰
cd ~/.ssh # cd到當前用戶的.ssh文件夾
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "注冊gitee郵箱" #為不同秘鑰指定名稱
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "注冊github郵箱"
完成后會在~/.ssh / 目錄下生成以下文件:
- id_rsa.github
- id_rsa.github.pub
- id_rsa.gitee
- id_rsa.gitee.pub
復制公鑰分別在github和gitee中設置
cat id_rsa.github.pub
cat id_rsa.gitee.pub
添加新的私鑰
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa.github
$ ssh-add ~/.ssh/id_rsa.gitee
3. 進行全局配置
touch ~/.ssh/config
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.gitee
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.github
Host 它涵蓋了下面一個段的配置,我們可以通過他來替代將要連接的服務器地址。 這里可以使用任意字段或通配符。 當ssh的時候如果服務器地址能匹配上這里Host指定的值,則Host下面指定的HostName將被作為最終的服務器地址使用,並且將使用該Host字段下面配置的所有自定義配置來覆蓋默認的/etc/ssh/ssh_config配置信息。
Port 自定義的端口。默認為22,可不配置
User 自定義的用戶名,默認為git,可不配置
HostName 真正連接的服務器地址
PreferredAuthentications 指定優先使用哪種方式驗證,支持密碼和秘鑰驗證方式
IdentityFile 指定本次連接使用的密鑰文件
4. 測試連接
ssh -T git@github.com
Hi yourname! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@gitee.com
Hi yourname! You've successfully authenticated, but GITEE.COM does not provide shell access.
5. hexo博客倉庫
vi .depoly_git/.git/config 增加
[user]
name = username
email = email
6. 針對不同的項目倉庫
增加本地配置,在每個倉庫的.git/config
中進行配置不同的用戶,以及其他的配置信息
$ git config --local user.name 'github/gitee賬號名'
$ git config --local user.email 'github/gitee賬號郵箱'
--global是在全局配置文件中設置
--local 是針對當前倉庫的項目進行設置