我正在新机器(m2 Mac)上设置我的开发环境,并设置了~/.gitconfig
~/.gitconfig-work
和~/.ssh/config
来分离我的ssh密钥,以供个人和专业使用。
~/.gitconfig
[user]
name = personal-username
email = personal-email@gmail.com
IdentityFile = ~/.ssh/id_rsa
[includeIf "gitdir:~/Documents/work/"]
path = ~/.gitconfig-work
[init]
defaultBranch = main
~/.gitconfig-work
[user]
name = work-username
email = work-email@work.com
IdentityFile = ~/.ssh/id_rsa_work
~/.ssh/config
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
# Work
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
IdentitiesOnly yes
我将密钥添加到ssh代理并且克隆工作正常,前提是我更新远程URL以具有正确的主机字段,例如git clone git@github-work:work/work-repo.git
现在我工作的公司有一些 npm 包,这是我们大多数项目的基础,简单地运行npm install
总是可以在我以前的 mac 上运行,但现在在运行 npm install 时,我收到以下错误
npm install
npm ERR! code 128
npm ERR! An unknown git error occurred
npm ERR! command git --no-replace-objects ls-remote ssh://git@github.com/work/work-private-repo.git
npm ERR! ERROR: Repository not found.
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR! A complete log of this run can be found in:
我使用node v16.16.0
和npm v8.11.0
,尝试使用节点 14 和 12,但都没有奏效。
我试图找出两台机器的开发设置之间的差异,但没有发现任何差异。
从错误来看,问题是在尝试调用时
git --no-replace-objects ls-remote ssh://git@github.com/work/work-private-repo.git
当我将远程调用更新到
git --no-replace-objects ls-remote ssh://git@github-work/work/work-private-repo.git
为了匹配我的工作配置文件和密钥,它将返回有效的响应。
找不到的存储库的 package.json 条目是
"work-private-repo": "git+https://github.com/work/work-private-repo.git",
TL;博士
我相信您想配置insteadOf
URL 技巧。 特别是,您需要:
git config --file ~/.gitconfig-work url.https://github-work/.insteadOf https://github.com/
例如。 (仔细检查所有这些,很容易弄错这些,我可能会有一些倒退。 另外,请随意直接编辑文件,而不是使用git config
。 放下user.IdentityFile
:它没有害处,但会让人感到困惑。
长
把:
[user]
IdentityFile = ~/.ssh/id_rsa_work
放入.gitconfig
文件中绝对不会做任何事情:没有错误,没有 Git 行为的变化,没有 SSH 行为的变化。 这是因为 Git 允许您将任意未使用的变量名称设置为任意值,而不会抱怨,然后它对这些变量不执行任何操作。user.IdentityFile
在 Git 中不使用。
您需要指定IdentityFile
(最好也设置IdentitiesOnly yes
)的位置在.ssh/config
文件中,因此这部分是完全正确的:
Host github.com-personal HostName github.com User git IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes # Work Host github-work HostName github.com User git IdentityFile ~/.ssh/id_rsa_work IdentitiesOnly yes
接下来你需要的是 Git 端:你需要创建一个包含引用github-work
的 URLgithub.com
,来自此处的第二部分,或从第一部分github.com-personal
。 (但第一个足够接近默认值,无论如何它可能已经可以工作了:如果你要求 ssh 连接到github.com
,没有Host
行,所以你连接到github.com
并使用默认标识 - 包括id_rsa
。
这就是insteadOf
设置的用武之地。 根据git config
文档:
url.<base>.insteadOf
任何以此值开头的 URL 都将被重写为以
开头。如果某些站点提供大量存储库,并使用多种访问方法为它们提供服务,并且某些用户需要使用不同的访问方法,则此功能允许人们指定任何等效的 URL,并让 Git 自动将 URL 重写为特定用户的最佳替代方案,即使对于站点上从未见过的存储库也是如此。当多个 instead Of 字符串与给定 URL 匹配时,将使用最长的匹配项。
通过将这些 URL 重写放入每个工作和每个家庭的设置中,您可以自动将标准样式、面向 HTTPS 的 URL 替换为个性化样式的 ssh URL 之一,并根据克隆在文件系统中的位置选择个性化 URL。
由于它都是前缀匹配,您可能还需要一个ssh://github.com/
条目:因为它只是告诉 Git 在使用https://github.com/
时切换到 ssh。通过这种方式,您将个性化 ssh URL 以及 HTTPS URL。
另请参阅如何让 git 默认为 ssh 而不是新存储库的 https。