本地同时使用多个git账号

config文件说明

Git Document指示在首次安装git的时候需要配置Config的相关内容信息,有三个地方存储了config文件,决定了读取的场景不同。

1 /etc/gitconfig:系统级别的gitconfig。包含所有当前系统上的user配置。如果git config命令使用了参数--system,将从这个文件的读和写。

输入:

 git config --system user.name "testname"

然后在F:\Git\mingw64\etc\gitconfig找到了user为testname的字符串。

2 ~/.gitconfig or ~/.config/git/config:全局的gitconfig, 通过 --global参数操作。

输入:

 git config --global user.name "testname"

然后在C:\Users\你的用户名\.gitconfig找到了user为testname的字符串。
由于这里需要在一个用户下使用多个git账号,所以把global的这个config文件删除了
或者使用命令

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

3 你的项目/.git/config: 每个项目单独的config文件。.git这个文件夹可能是隐藏的。

如果三个地方都设置了,优先级为:项目config>global>system

以上均在windows10上操作的结果

查看配置

git config --global --list
git config --system --list
git config list

hexo deploy的时候使用的global的config。要指定hexo的提交用户,修改global的config。

问题:Permission to user1/repo.git denied to user2

解决步骤

  1. 查看_config.yml文件中git库地址是不是https的,如果是修改成ssh的。
  2. 为不同的项目生成多个ssh key

控制台输入参数

步骤1 ssh key,cd到目录~/.ssh/(会在当前目录下创建),输入:

ssh-keygen -t rsa -C "git账号邮箱"

出现如下提示

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/luna/.ssh/id_rsa):

这里输入一个存放key的名称(如不输入直接回车使用的是默认的名称id_rsa),输入后出现提示

Enter passphrase (empty for no passphrase):

直接回车,出现提示

Enter same passphrase again:

直接回车,出现成功提示,如下,则说明新建成功

Your identification has been saved in "这里是之前输入的名称".
Your public key has been saved in "这里是之前输入的名称".pub.
The key fingerprint is:
SHA256:cAK6PaYB7JWoHnkoF453D1weQjTYqsdDE23M "这里是之前输入的邮箱"
The key's randomart image is:
+---[RSA 2048]----+
|+AA+uu           |
|@B*=*.           |
|B+=..o .        |
|oo+++  +         |
|o=-.  S        |
|o= .           |
|+.o              |
|               |
| ..              |
+----[SHA256]-----+

直接一次性输入参数

生成ssh key时同时指定保存的文件名

ssh-keygen -t rsa -f ~/.ssh/id_rsa_first -C "邮件地址"

之后的两个提示直接回车

上面的id_rsa_first就是我们指定的文件名,这时~/.ssh目录下会多出id_rsa_first和id_rsa_first.pub两个文件,id_rsa_first.pub里保存的就是我们要使用的key。

查看ssh key

cat ~/.ssh/id_rsa_first.pub

注意:修改了当前账户的ssh key,有时没生效,需要重新新建文件夹来git init。

配置项目的git用户名和邮箱

git config user.name "用户名"
git config user.email "邮箱"

新增并配置config文件

如果config文件存在则直接修改,不存在则先新建一个config文件

touch ~/.ssh/config

如果是在windows下新建一个txt文本,然后将名字后缀一起改成config即可

配置config文件

在config文件里添加如下内容(User表示你的用户名)

# first
Host first.github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_first
User first_user

# second
Host second.github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_second
User second_user

hostname为host转化后的域名

注意:这里windows下面IdentityFile路径也是用反斜杠

修改remote url git@ 后面的需要与host一样
直接修改config文件:打开项目->.git->config文件

url = [email protected]:xxx/xxx.git

修改为:

url = [email protected]:xxx/xxx.git

或者使用命令行显示并修改remoteurl

根据上面的配置,就要把github.com换成first.github.com, 那么ssh解析的时候就会自动把first.github.com 转换为 github.com

添加key到项目

根据上面生成的key添加到对应的项目中。

测试ssh key是否配置成功

ssh -T [email protected]
ssh -T [email protected]

这里不要后面的xxx.git

如果需要打印详细信息使用-v

ssh -v [email protected]
ssh -v [email protected]

如果配置成功,则会显示:
Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.

解析步骤

  1. 当push变更时,首先拿到当前项目的remote url如[email protected]:xxx/xxx.git
  2. 然后在~/.ssh/config中寻找对应的真实的url替换成真实地址[email protected]:xxx/xxx.git
  3. 拿到对应的identiyFile对应的ssh文件中的key提交,完成。

所以在部署hexo的时候需要把__config例的repo地址修改为[email protected]:xxx/xxx.git

clone某个项目的时候也需要把url修改:

git clone [email protected]:xxx/xxx.git

修改为

git clone [email protected]:xxx/xxx.git

错误

ssh_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.
OpenSSH_7.6p1, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
ssh: Could not resolve hostname source.github.com:xxx/xxx.git: Name or service not known

这里没有找到~/user/.ssh/config文件,所以找到了系统的ssh_config文件,发现没有相关配置项。

新建config文件时新建成了config.config,ssh 客户端找不到config文件,所以读取了系统的/etc/ssh/ssh_config

猜你喜欢

转载自www.cnblogs.com/for-you/p/12761829.html