.gitconfig通常存储在用户中。主目录。
我使用不同的身份为a公司工作,为B公司工作(主要是名称/电子邮件)。如何使用两种不同的Git配置,使我的签入与名称/电子邮件不一致?
.gitconfig通常存储在用户中。主目录。
我使用不同的身份为a公司工作,为B公司工作(主要是名称/电子邮件)。如何使用两种不同的Git配置,使我的签入与名称/电子邮件不一致?
当前回答
从git 2.13版本开始,git支持条件配置包含。在这个例子中,我们克隆了A公司在~/company_a目录下的回购,以及B公司在~/company_b目录下的回购。
在.gitconfig文件的末尾,你可以这样写:
[includeIf "gitdir:~/company_a/"]
path = .gitconfig-company_a
[includeIf "gitdir:~/company_b/"]
path = .gitconfig-company_b
.gitconfig-company_a的示例内容(如果可以使用全局ssh密钥,可以省略[core]部分):
[user]
name = John Smith
email = john.smith@companya.net
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_companya
.gitconfig-company_b的示例内容:
[user]
name = John Smith
email = js@companyb.com
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_companyb
其他回答
您可以通过更改存储库特定的配置文件(即/path/to/repo/. Git /config)来定制项目的Git配置。BTW,默认情况下,git配置会写入这个文件:
cd /path/to/repo
git config user.name 'John Doe' # sets user.name locally for the repo
我更喜欢为不同的项目创建单独的配置文件(例如在~/.gitconfig.d/),然后将它们包含在存储库的配置文件中:
cd /path/to/repo
git config include.path '~/.gitconfig.d/myproject.conf'
如果您需要在属于单个项目的多个回购中使用相同的选项集,那么这种方法很有效。您还可以设置shell别名或自定义Git命令来操作概要文件。
从git 2.13版本开始,git支持条件配置包含。在这个例子中,我们克隆了A公司在~/company_a目录下的回购,以及B公司在~/company_b目录下的回购。
在.gitconfig文件的末尾,你可以这样写:
[includeIf "gitdir:~/company_a/"]
path = .gitconfig-company_a
[includeIf "gitdir:~/company_b/"]
path = .gitconfig-company_b
.gitconfig-company_a的示例内容(如果可以使用全局ssh密钥,可以省略[core]部分):
[user]
name = John Smith
email = john.smith@companya.net
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_companya
.gitconfig-company_b的示例内容:
[user]
name = John Smith
email = js@companyb.com
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_companyb
找到了一个非常有用的shell脚本,覆盖了ssh密钥生成的所有过程,并一次又一次地输入长命令
注意:这仅适用于ZSh用户
https://github.com/tw-yshuang/Git_SSH-Account_Switch
遵循以下步骤:
从系统中找到.gitconfig Windows文件位置:"C:\Users${USER_NAME}.gitconfig" Linux文件路径:“/usr/local/git/etc/gitconfig” 打开.gitconfig文件,并根据您的条件添加以下行 [includeIf“gitdir: D: \ ORG-A-PROJECTS \”) (用户) 约翰·史密斯 email = js@organizationx.com [includeIf "gitdir:~/organization_b/"] (用户) 无名氏 邮箱= jd@organizationy.com
由于@crea1
一个小变种:
这篇文章写在https://git-scm.com/docs/git-config#_includes:上
如果模式以/结尾,则会自动添加**。例如,模式foo/变成了foo/**。换句话说,它递归地匹配foo和里面的所有东西。
在我的例子中, ~ /。gitconfig:
[user] # as default, personal needs
email = myalias@personal-domain.fr
name = bcag2
[includeIf "gitdir:~/workspace/"] # job needs, like workspace/* so all included projects
path = .gitconfig-job
# all others section: core, alias, log…
因此,如果项目目录在我的~/wokspace/,默认用户设置将替换为 ~ /。gitconfig-job:
[user]
name = John Smith
email = js@company.com