首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按目录分开gitconfig

按目录分开gitconfig
EN

Stack Overflow用户
提问于 2021-11-20 08:16:21
回答 1查看 148关注 0票数 4

我有两个不同的github帐户,一个工作,一个个人项目。在我的笔记本电脑上,我创建了两个不同的目录来克隆我的Github存储库:

Perso/Users/pierre-alexandre/Documents/perso

Work/Users/pierre-alexandre/Documents/work

然后,我在/Users/pierre-alexandre/.ssh上生成了两个不同的SSH密钥,并在各自的.pub存储库中添加了每个.pub密钥。最后,这是我的/Users/pierre-alexandre/.ssh文件夹的样子:

代码语言:javascript
复制
 id_perso    id_perso.pub   id_work    id_work.pub   known_hosts

之后,我所做的是:

代码语言:javascript
复制
ssh-add ~/.ssh/id_work
ssh-add ~/.ssh/id_perso

然后,我创建了一个新文件~/.gitconfig

代码语言:javascript
复制
[includeIf "gitdir:~/Documents/perso/"]
  path = ~/.gitconfig-perso
[includeIf "gitdir:~/Documents/servier/"]
  path = ~/.gitconfig-servier

在此之后,我创建了另外两个不同的文件,并确保我的名字与我的Github用户名相对应,而电子邮件对应于我的Github电子邮件+我用来生成我的SSH密钥的邮件:

~/..gitconfig work

代码语言:javascript
复制
[user]
 name = pamousset35
 email = work-email

~/..gitconfig perso

代码语言:javascript
复制
[user]
 name = Pierre-Alexandre35
 email = name@gmail.com

我的问题是:我能够与我的工作互动,但是我的个人github,我总是有这个问题,当我试图pushpull

代码语言:javascript
复制
pwd
/Users/pierre-alexandre/Documents/perso/pierre-alexandre.io
git pull 

错误日志:

代码语言:javascript
复制
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

使用git config -l 的github配置(在同一目录下)

代码语言:javascript
复制
credential.helper=osxkeychain
includeif.gitdir:~/Documents/perso/.path=~/.gitconfig-perso
user.name=Pierre-Alexandre35
user.email=pamousset75@gmail.com
includeif.gitdir:~/Documents/servier/.path=~/.gitconfig-servier
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=git@github.com:Pierre-Alexandre35/pierre-alexandre.io.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.develop.remote=origin
branch.develop.merge=refs/heads/develop

然后我还输入了:ssh -vT git@github.com

代码语言:javascript
复制
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/pierre-alexandre/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/pierre-alexandre/.ssh/id_rsa type -1
debug1: identity file /Users/pierre-alexandre/.ssh/id_rsa-cert type -1
debug1: identity file /Users/pierre-alexandre/.ssh/id_dsa type -1
debug1: identity file /Users/pierre-alexandre/.ssh/id_dsa-cert type -1
debug1: identity file /Users/pierre-alexandre/.ssh/id_ecdsa type -1
debug1: identity file /Users/pierre-alexandre/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/pierre-alexandre/.ssh/id_ed25519 type -1
debug1: identity file /Users/pierre-alexandre/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/pierre-alexandre/.ssh/id_xmss type -1
debug1: identity file /Users/pierre-alexandre/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version babeld-a73e1397
debug1: no match: babeld-a73e1397
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM
debug1: Host 'github.com' is known and matches the ECDSA host key.
debug1: Found key in /Users/pierre-alexandre/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /Users/pierre-alexandre/.ssh/id_rsa 
debug1: Will attempt key: /Users/pierre-alexandre/.ssh/id_dsa 
debug1: Will attempt key: /Users/pierre-alexandre/.ssh/id_ecdsa 
debug1: Will attempt key: /Users/pierre-alexandre/.ssh/id_ed25519 
debug1: Will attempt key: /Users/pierre-alexandre/.ssh/id_xmss 
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa,ssh-dss>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_rsa
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_dsa
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_ecdsa
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_ed25519
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_xmss
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey)

然后是eval "$(ssh-agent -s)"

代码语言:javascript
复制
Agent pid 21213

ssh-add -l

代码语言:javascript
复制
The agent has no identities.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-20 09:48:03

Git只是运行ssh来连接到主机。一旦连接到Git,Git就让ssh在其端运行一个适当的Git命令,以处理获取或推送操作。但是,整个身份验证过程--确定您是谁并决定您是否有访问权限--完全取决于ssh和Git在这个过程中没有真正的作用。

因此,您的ssh -Tv是这里的关键调试输出。我们看到,在尝试以下密钥之后,您与github的连接无法进行身份验证:

代码语言:javascript
复制
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_rsa
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_dsa
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_ecdsa
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_ed25519
debug1: Trying private key: /Users/pierre-alexandre/.ssh/id_xmss

请注意,它从未尝试过任何公钥。您需要将ssh提供给GitHub,这是正确的公钥。

在这一点上,还有另一个问题即将出现。您的URL是:

代码语言:javascript
复制
remote.origin.url=git@github.com:Pierre-Alexandre35/pierre-alexandre.io.git

也就是说,您将让Git请求以用户git身份在主机github.com上登录。您没有~/.ssh/config,所以没有神奇的覆盖: ssh将连接到github.com并请求以git身份登录,然后提供标准的ssh密钥集。但是您需要一个覆盖:您希望连接到github.com并请求以git身份登录,但是您希望ssh:

  • 发送id_perso公钥的当且仅当您来自个人存储库,或者
  • 发送id_work公钥的当且仅当您来自工作存储库

ssh不知道您来自哪种存储库。

你需要以某种方式走私这种储存库--最好是以一种聪明的方式。稍后我们将回到聪明的地方,但是现在让我们假设您只是在每个工作存储库中使用git remote set-url origin来更改日志--从git@github.comgit@gh-work的URL的一部分,以及个人存储库的URL的登录部分到git@gh-perso

然后,在~/.ssh/config文件中,可以列出以下内容:

代码语言:javascript
复制
Host gh-work
    Hostname github.com
    IdentityFile ~/.ssh/id_work.pub
    IdentitiesOnly yes

Host gh-perso
    Hostname github.com
    IdentityFile ~/.ssh/id_perso.pub
    IdentitiesOnly yes

这配置ssh,完全独立于Git,例如,当您运行ssh git@gh-perso时,可以告诉ssh它应该:

  • 连接到github.com (将id_perso.pub文件替换为键;
  • 只尝试列出的IdentityFiles (可以列出多个),即跳过标准文件.

因此,当Git要求ssh连接到git@gh-perso时,ssh实际上试图连接到github.com

(附带说明,如果将User git添加到HostIdentityFile等部分,则可以取消git@部件。这不是必需的,但有点方便。)

注意:有时您可能或必须列出私钥文件,而不是公钥文件。有时,您必须只列出公钥文件。这似乎取决于代理版本和/或ssh版本。使用ssh -Tv git@gh-perso.com,直到该部分正常工作为止,然后返回到Git方面。

通过Git includeIf使它变得聪明和自动

Git有一个窍门,它会在“最后一刻”重写URL,然后再与fetch或push连接。给定任何URL,Git都会查找insteadOf条目。例如,这在How to convert git: urls to http: urls中有解释,但我们可以使用它重写:

代码语言:javascript
复制
ssh://github.com/Pierre-Alexandre35/pierre-alexandre.io.git

转入:

代码语言:javascript
复制
ssh://gh-perso/Pierre-Alexandre35/pierre-alexandre.io.git

我们只需将insteadOf设置为:

代码语言:javascript
复制
[url "ssh://gh-perso/"]
    insteadOf = ssh://github.com/

例如。(我并没有实际测试所有的insteadOf内容,您可能需要稍微修改一下细节,这取决于您是否使用git@,以及是否使用git@github.com:表单而不是ssh://git@github.com表单。)将适当的重写规则放入每个包含的个人和工作配置文件中,现在github.com URL自动变成gh-persogh-work URL,然后触发使用ssh设置的内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70044276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档