是否可以为不同的项目使用不同的git配置

问题描述 投票:209回答:9

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

我使用不同的身份来处理CompanyA的项目以及CompanyB的其他内容(主要是名称/电子邮件)。我如何才能拥有2种不同的git配置,以便我的签到不会使用名称/电子邮件?

git git-config
9个回答
202
投票

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

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


223
投票

git配置有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" 

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


149
投票

从git版本2.13开始,git支持conditional configuration includes。在这个例子中,我们克隆了公司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的示例内容

[user]
name = John Smith
email = [email protected]

.gitconfig-company_b的示例内容

[user]
name = John Smith
email = [email protected]

12
投票

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

git config --global alias.hobbyprofile 'config user.email "[email protected]"'

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


9
投票

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


6
投票

谢谢@ crea1

一个小变种:

正如它写在https://git-scm.com/docs/git-config#_includes

如果模式以/结尾,则会自动添加**。例如,模式foo/变成foo/**。换句话说,它以递归方式匹配foo和内部的所有内容。

所以我在我的情况下使用:

[user] # as default, personal needs
    email = [email protected]
    name = bcag2
[includeIf "gitdir:~/workspace/"] # job needs, like workspace/* so all included projects
    path = .gitconfig-job

因此,如果项目目录位于我的~/wokspace/中,则默认用户设置将替换为:

[user]
name = John Smith
email = [email protected]

5
投票

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

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

1
投票

我在同一条船上。我写了一个小的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 <[email protected]>

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

0
投票

我在尝试git stash我的本地更改时遇到了错误。来自git的错误说“请告诉我你是谁”,然后告诉我“运行git config --global user.email "[email protected]git config --global user.name "Your name"来设置你帐户的默认身份。”但是,您必须Omit --global仅在当前存储库中设置标识。

© www.soinside.com 2019 - 2024. All rights reserved.