485

.gitconfig通常存放在user.home目录中。

我使用不同的身份为 A 公司处理项目,为 B 公司处理其他项目(主要是姓名/电子邮件)。我怎样才能拥有两种不同的 Git 配置,以便我的签到不使用姓名/电子邮件?

4

13 回答 13

432

git config 有 3 个级别;项目、全局和系统。

  • project:项目配置仅适用于当前项目,并存储在项目目录中的 .git/config 中。
  • global:全局配置可用于当前用户的所有项目,并存储在 ~/.gitconfig 中。
  • system:系统配置可用于所有用户/项目,并存储在 /etc/gitconfig 中。

创建一个项目特定的配置,你必须在项目目录下执行:

$ git config user.name "John Doe" 

创建一个全局配置:

$ git config --global user.name "John Doe"

创建系统配置:

$ git config --system user.name "John Doe" 

正如您可能猜到的那样,项目覆盖全局和全局覆盖系统。

注意:项目配置对于这个特定 repo 的一个特定副本/克隆来说是本地的,如果 repo 从远程重新克隆干净,则需要重新应用。它通过提交/推送更改未发送到远程的本地文件。

于 2013-05-22T02:17:48.513 回答
375

从 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 密钥,可以省略核心部分)

[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
于 2017-05-10T06:06:53.133 回答
361

.git/config存储库的特定克隆中的文件是该克隆的本地文件。放置在那里的任何设置只会影响该特定项目的操作。

(默认情况下,git config修改.git/config,而不是~/.gitconfig- 仅--global修改后者。)

于 2012-01-10T10:27:24.143 回答
77

谢谢@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…

因此,如果项目目录位于 my 中~/wokspace/,则默认用户设置将替换为
~/.gitconfig-job

[user]
name = John Smith
email = js@company.com
于 2019-01-10T09:50:46.620 回答
24

明确地说,您还可以--local使用当前存储库配置文件

git config --local user.name "John Doe" 

或者正如@SherylHohman 提到的,使用以下命令在编辑器中打开本地文件:

git config --local --edit
于 2018-10-26T17:41:17.683 回答
16

我正在通过以下方式为我的电子邮件执行此操作:

git config --global alias.hobbyprofile 'config user.email "me@example.com"'

然后,当我克隆一个新的工作项目时,我只需运行git hobbyprofile它,它将被配置为使用该电子邮件。

于 2016-10-16T06:55:04.900 回答
14

另一种方法是使用direnv并将每个目录的配置文件分开。例如:

.
├── companyA
│  ├── .envrc
│  └── .gitconfig
├── companyB
│  ├── .envrc
│  └── .gitconfig
└── personal
   ├── .envrc
   └── .gitconfig

每个都.envrc应该包含这样的内容:

export GIT_CONFIG_GLOBAL=$(pwd)/.gitconfig

并且.gitconfig通常是具有所需值的 gitconfig 。

这是我在自定义.gitconfig文件中实际拥有的:

[user]
    email = my.name@company.com

[include]
    path = ~/.gitconfig

这里仅user.email被覆盖,其余配置取自默认配置~/.gitconfig

于 2019-11-08T14:32:27.647 回答
13

您还可以将环境变量指向应该使用GIT_CONFIG的文件。git config随着GIT_CONFIG=~/.gitconfig-A git config key value指定的文件被操纵。

于 2012-01-10T10:50:59.847 回答
4

您可以通过更改存储库特定配置文件(即/path/to/repo/.git/config)来自定义项目的 Git 配置。顺便说一句,git config默认情况下写入此文件:

cd /path/to/repo
git config user.name 'John Doe'  # sets user.name locally for the repo

我更喜欢为不同的项目(例如 in ~/.gitconfig.d/)创建单独的配置文件,然后它们包含在存储库的配置文件中:

cd /path/to/repo
git config include.path '~/.gitconfig.d/myproject.conf'

如果您需要在属于单个项目的多个存储库中使用相同的选项集,这很有效。您还可以设置 shell 别名或自定义 Git 命令来操作配置文件。

于 2019-12-16T09:00:58.957 回答
1

按照步骤:

  1. 从系统中查找 .gitconfig

    Windows 的文件位置: “C:\Users${USER_NAME}.gitconfig”

    Linux 的文件位置: “/usr/local/git/etc/gitconfig”

  2. 打开 .gitconfig 文件并根据您的条件添加以下行

     [includeIf "gitdir:D:\ORG-A-PROJECTS\"]
    
     [user]
    
         name = John Smith
    
         email = js@organizationx.com [includeIf "gitdir:~/organization_b/"]
    
     [user]
    
         name = John Doe
    
         email = jd@organizationy.com
    
于 2020-08-20T09:02:04.540 回答
0

我在同一条船上。我写了一个小 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
于 2012-05-16T00:08:06.837 回答
0

尝试进行git stash本地更改时出错。来自 git 的错误说“请告诉我你是谁”,然后告诉我“运行git config --global user.email "you@example.comgit config --global user.name "Your name"设置您帐户的默认身份”。但是,您必须省略 --global以仅在当前存储库中设置身份。

于 2019-03-26T20:03:18.670 回答
0

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

2021 年 8 月 13 日之后,git 不支持 HTTPS 身份验证方法,所以我相信这个答案需要更新。

请按照以下步骤操作:

  • 删除目录中存储的 Git 的所有 SSH 密钥(公共和私有)~/.ssh/
  • 创建新的 SSH 密钥:
ssh-keygen -t rsa -b 4096 -C "account1@gmail.com"
ssh-keygen -t rsa -b 4096 -C "account2@gmail.com"

当询问文件名时,分别~/.ssh/id_rsa为 account1 和~/.ssh/id_rsa_acc2account2 提供默认值。

  • 加星 ssh-agent 并为 account1 和 account2 添加私钥:
eval `ssh-agent -s
ssh-add -k ~/.ssh/id_rsa
ssh-add -k ~/.ssh/id_rsa_acc2

确认键是通过命令添加的ssh-add -l

  • 复制 account1 和 account2 的公钥并将其添加到您的 github 帐户。

  • 将 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 帐户 2,请在项目根目录中的项目级别设置用户名和电子邮件:
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

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

于 2022-01-27T06:11:35.037 回答