.gitconfig通常存储在用户中。主目录。
我使用不同的身份为a公司工作,为B公司工作(主要是名称/电子邮件)。如何使用两种不同的Git配置,使我的签入与名称/电子邮件不一致?
.gitconfig通常存储在用户中。主目录。
我使用不同的身份为a公司工作,为B公司工作(主要是名称/电子邮件)。如何使用两种不同的Git配置,使我的签入与名称/电子邮件不一致?
当前回答
另一种方法是使用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
存储库的特定克隆中的.git/config文件是该克隆的本地文件。放置在那里的任何设置只会影响该特定项目的操作。
(默认情况下,git配置修改的是.git/config,而不是~/。Gitconfig -仅使用——global会修改后者。)
遵循以下步骤:
从系统中找到.gitconfig Windows文件位置:"C:\Users${USER_NAME}.gitconfig" Linux文件路径:“/usr/local/git/etc/gitconfig” 打开.gitconfig文件,并根据您的条件添加以下行 [includeIf“gitdir: D: \ ORG-A-PROJECTS \”) (用户) 约翰·史密斯 email = js@organizationx.com [includeIf "gitdir:~/organization_b/"] (用户) 无名氏 邮箱= jd@organizationy.com
另一种方法是使用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。
git配置有3个级别;项目、全局、系统。
项目:项目配置仅对当前项目可用,并存储在项目目录下的.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,则需要重新应用。它修改没有通过提交/推送发送到远程的本地文件。