如何设置git config以将特定密钥用于域?

时间:2019-09-04 22:36:16

标签: git ssh gitlab

您如何在~/.gitconfig中制定规则,告诉git为特定项目使用特定的SSH密钥?

我在Gitlab上有单独的个人和专业帐户,并且在提交到大多数Gitlab存储库时想使用我的个人SSH密钥,但是在提交给我的专业Gitlab存储库时我想指定一个单独的密钥。除了保持整洁外,从技术角度来看这也是必需的,因为Gitlab不允许您在两个帐户之间共享SSH密钥。

在搜索这种情况时,我发现了许多解决方案,例如将其附加到我的~/.gitconfig

[includeIf "gitdir:~/git/my_professional_project/"]
    [user]
        email = "myuser@domain.com"
    [core]
        sshCommand = "ssh -i ~/.ssh/id_rsa_myuser_at_domain.pub"

从理论上讲,这告诉git,每当在目录~/git/my_professional_project/中进行提交以使用密钥~/.ssh/id_rsa_myuser_at_domain.pub通过SSH推送提交时。

我发现这确实允许git提交到我的专业帐户。但是,尽管有“ IncludeIf”语句,但我发现它的副作用是它也使git对所有git提交都使用我的专业密钥,从而导致臭名昭著的错误:

You are not allowed to push code to this project.
fatal: Could not read from remote repository.

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

includeIf规则语法是否有错误?为什么此配置会强制git使用错误的SSH密钥?

2 个答案:

答案 0 :(得分:0)

includeIf指令/节仅使用一个名为path的字段:

[includeIf "expression"]
    path = /path/to/file

您不能在此文件中添加coreuser 。您必须将[core][user]指令放在另一个配置文件中。然后,将includeIfpath一起使用Dim lastrow As Long Dim nextrow As Long Set sht = ThisWorkbook.Worksheets("Sheet1") lastrow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row nextrow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row + 1 'assigns objects for the last filled row and the next unfilled row wb1.Worksheets("Database").Range("B1:B578").Copy Workbooks("zzmaster.xlsx").Worksheets("Sheet1").Cells(nextrow, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True DoEvents Application.CutCopyMode = False 'copies the relevant data from Column B in Sheet 3 of the source file... 'then transposes and pastes into the next available row of the destination file wb1.Worksheets("Database").Range("C32:C578").Copy Workbooks("zzmaster.xlsx").Worksheets("Sheet1").Cells(lastrow, 579).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True DoEvents Application.CutCopyMode = False 'copies the relevant data from Column C in Sheet 3 of the source file... 'then tranposes and pastes into the same row as the previous function, beginning with... 'the next cell after the last data point 指令(您的现有条件很好)来告诉Git在哪里可以找到其他文件

答案 1 :(得分:0)

您的includeIf语法确实不正确。您写的是没有配置设置的includeIf部分,然后是带有user设置的email部分,然后是带有{{1} }设置,尽管缩进有些奇怪。

如果要使用core部分,则需要将其他选项放在单独的文件中,并包括以下内容:

sshCommand