.gitconfig通常存储在用户中。主目录。
我使用不同的身份为a公司工作,为B公司工作(主要是名称/电子邮件)。如何使用两种不同的Git配置,使我的签入与名称/电子邮件不一致?
.gitconfig通常存储在用户中。主目录。
我使用不同的身份为a公司工作,为B公司工作(主要是名称/电子邮件)。如何使用两种不同的Git配置,使我的签入与名称/电子邮件不一致?
当前回答
我在我的电子邮件中这样做的方式如下:
git config --global alias.hobbyprofile 'config user.email "me@example.com"'
然后当我克隆一个新的工作项目时,我只需要运行git hobbyprofile,它将被配置为使用该电子邮件。
其他回答
另一种方法是使用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
这里只有用户。Email将被覆盖,其余配置取自默认的~/.gitconfig。
我也有同感。我写了一个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
您可以通过更改存储库特定的配置文件(即/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命令来操作概要文件。
由于@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
你也可以将环境变量GIT_CONFIG指向一个git配置应该使用的文件。与GIT_CONFIG = ~ /。git配置-指定文件的git配置键值。