.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

其他回答

我也有同感。我写了一个bash脚本来管理它们。 https://github.com/thejeffreystone/setgit

#!/bin/bash

# setgit
#
# Script to manage multiple global gitconfigs
# 
# To save your current .gitconfig to .gitconfig-this just run:
# setgit -s this
#
# To load .gitconfig-this to .gitconfig it run:
# setgit -f this
# 
# 
# 
# Author: Jeffrey Stone <thejeffreystone@gmail.com>

usage(){
  echo "$(basename $0) [-h] [-f name]" 
  echo ""
  echo "where:"
  echo " -h  Show Help Text"
  echo " -f  Load the .gitconfig file based on option passed"
  echo ""
  exit 1  
}

if [ $# -lt 1 ]
then
  usage
  exit
fi

while getopts ':hf:' option; do
  case "$option" in
      h) usage
         exit
         ;;
      f) echo "Loading .gitconfig from .gitconfig-$OPTARG"
         cat ~/.gitconfig-$OPTARG > ~/.gitconfig
         ;;
      *) printf "illegal option: '%s'\n" "$OPTARG" >&2
         echo "$usage" >&2
         exit 1
         ;;
    esac
done

你也可以将环境变量GIT_CONFIG指向一个git配置应该使用的文件。与GIT_CONFIG = ~ /。git配置-指定文件的git配置键值。

使用Github配置添加多个SSH密钥

2021年8月13日之后,git不支持HTTPs认证方法,所以我认为这个答案需要更新。

遵循以下步骤:

删除保存在~/. SSH /目录下的所有SSH密钥(公共和私有)。 创建新的SSH密钥:

ssh-keygen -t rsa -b 4096 -C "account1@gmail.com"
ssh-keygen -t rsa -b 4096 -C "account2@gmail.com"

当询问文件名时,给出默认的~/。为account1和~/配置Ssh /id_rsa。Ssh /id_rsa_acc2对应account2。

启动ssh-agent,为account1和account2添加私钥:

eval `ssh-agent`
ssh-add -k ~/.ssh/id_rsa
ssh-add -k ~/.ssh/id_rsa_acc2

使用ssh-add -l命令确认添加密钥

复制account1和account2的公钥,并将其添加到您的github帐户。命令:cat ~/.ssh/id_rsa。Pub | pbcopy(检查是否复制:pbpaste 将account1的用户名和用户邮箱设置为GitHub全局配置:

git config --global user.name "acc1_username"
git config --global user.email "account1@gmail.com"

创建~ /。Ssh /config文件,配置如下:

# account1 github
Host github.com
HostName github.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa

# account2 github
Host github.com-acc2
HostName github.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa_acc2

要为一个项目设置GitHub account2,在项目根目录中设置项目级别的用户名和电子邮件:

git config user.name "acc2_username"
git config user.email "account2@gmail.com"

现在通过使用GitHub repo的SSH链接克隆或添加起源:

# for account1 repo
git remote set-url origin git@github.com:acc1_username/reponame.git

# for account2 repo
git clone git@github.com-acc2:acc2_username/reponame.git

如果有任何疑问,请随意添加评论。

您可以通过更改存储库特定的配置文件(即/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/config文件是该克隆的本地文件。放置在那里的任何设置只会影响该特定项目的操作。

(默认情况下,git配置修改的是.git/config,而不是~/。Gitconfig -仅使用——global会修改后者。)