同时使用gitee和github

https://juejin.im/post/6844904180633600007

https://www.jianshu.com/p/68578d52470c

Git共有三个级别的config文件,分别是:

  • system :%GitPath%mingw64etcgitconfig文件
  • global:$home.gitconfig文件
  • local:%RepoPath%.gitconfig文件

其中%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 是针对当前仓库的项目进行设置

原文地址:https://www.cnblogs.com/land-fill/p/13510396.html