.gitconfig
通常存储在user.home
目录中。
我使用不同的身份来处理CompanyA的项目以及CompanyB的其他内容(主要是名称/电子邮件)。我如何才能拥有2种不同的git配置,以便我的签到不会使用名称/电子邮件?
存储库的特定克隆中的.git/config
文件是该克隆的本地文件。放置在那里的任何设置只会影响该特定项目的操作。
(默认情况下,git config
修改.git/config
,而不是~/.gitconfig
- 仅使用--global
修改后者。)
git配置有3个级别;项目,全球和系统。
创建项目特定的配置,您必须在项目的目录下执行:
$ git config user.name "John Doe"
创建全局配置:
$ git config --global user.name "John Doe"
创建系统配置:
$ git config --system user.name "John Doe"
正如您可能猜到的,项目会覆盖全局和全局覆盖系统。
从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]
我通过以下方式为我的电子邮件执行此操作:
git config --global alias.hobbyprofile 'config user.email "[email protected]"'
然后,当我克隆一个新的工作项目时,我只需运行git hobbyprofile
,它将被配置为使用该电子邮件。
您还可以将环境变量GIT_CONFIG
指向git config
应该使用的文件。使用GIT_CONFIG=~/.gitconfig-A git config key value
,指定的文件会被操纵。
谢谢@ 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]
要明确,您还可以使用--local
来使用当前存储库配置文件:
git config --local user.name "John Doe"
我在同一条船上。我写了一个小的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
我在尝试git stash
我的本地更改时遇到了错误。来自git的错误说“请告诉我你是谁”,然后告诉我“运行git config --global user.email "[email protected]
和git config --global user.name "Your name"
来设置你帐户的默认身份。”但是,您必须Omit --global仅在当前存储库中设置标识。